【问题标题】:How i call IntentService from Broadcast Receiver?我如何从广播接收器调用 IntentService?
【发布时间】:2012-12-07 12:41:08
【问题描述】:

我为下载数据创建了一个意图服务,我想用广播接收和警报管理器重复。我如何调用我的意图服务?我尝试了这个但没有工作....

public class MyBroadcastReceiver extends BroadcastReceiver {


    @Override

      public void onReceive(Context context, Intent intent) {

          Toast.makeText(context, "Don't panik but your time is up!!!!.",
            Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator)   
context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(500);

        intent.setData(Uri.parse("http://www.mysite.net/LEDstate.txt"));
        intent.putExtra("urlpath", "http://www.mysite.net/LEDstate.txt");
        //startService(intent);

        context.startService(new Intent(context, DownloadService.class));

        }

公共类 AlarmActivity 扩展 Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void startAlert(View view) {

    EditText text = (EditText) findViewById(R.id.time);

    int i = Integer.parseInt(text.getText().toString());

    Intent intent = new Intent(this, MyBroadcastReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    //alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
       // + (i * 1000),pendingIntent);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                 ,(i*1000),pendingIntent);

   // alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar
    //      .getTimeInMillis(), intervals, pendingIntent);


    Toast.makeText(this, "Alarm set in " + i + " seconds",
        Toast.LENGTH_LONG).show();
  }

【问题讨论】:

  • 您在清单中为此类 DownloadService 定义了什么?
  • 是的 manifest 没问题。我尝试转换 vogella 教程 intentservice 每 x 秒重播一次。
  • 你没有尝试@Zzokk的解决方案吗?对我来说看起来不错。

标签: android broadcastreceiver intentservice


【解决方案1】:

试试这个:

Intent newIntent = new Intent(context, DownloadService.class)
newIntent.setData(Uri.parse("http://www.mysite.net/LEDstate.txt"));
newIntent.putExtra("urlpath", "http://www.mysite.net/LEDstate.txt");


context.startService(newIntent);

【讨论】:

    【解决方案2】:

    在您的代码中,您没有将数据传递给您的Service。您正在向Intent 对象添加数据和附加功能,将不同的Intent 对象传递给startService() 方法。使用@Zzokk 在另一篇文章中建议的解决方案,它应该可以工作。

    【讨论】:

      【解决方案3】:

      收到您的广播后,将调用您的广播活动。

      那时你可以随心所欲地调用任何活动。

      见以下代码:

       @Override
          public void onReceive(Context context, Intent intent) {
      
              AM_EM_APRIL_2012 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                      Intent in1 = new Intent(context, AlarmReceiverNotificationForEveryMonth.class);
                      in1.putExtra("MyMessage","PAYE payment and employer monthly schedule for March");
                      PI_EM_APRIL_2012 = PendingIntent.getBroadcast(context, 1, in1, PendingIntent.FLAG_UPDATE_CURRENT);
                      AM_EM_APRIL_2012.set(AlarmManager.RTC_WAKEUP, calendar_PAYE_20_APRIL_2012.getTimeInMillis(),PI_EM_APRIL_2012);
                  }
      

      在上面的代码中,AlarmReceiverNotificationForEveryMonth.class 是被调用的活动 如果您想在消息被广播时调用活动,这就是您必须做的。届时您可以根据需要启动相应的服务。

      希望你明白我的意思。

      【讨论】:

      【解决方案4】:

      你没有在onCreate函数中调用startAlert方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多