如何从广播接收器调用IntentService?

5

我创建了一个用于下载数据的Intent Service,并想要使用Broadcast Receiver和Alarm Manager来重复执行。如何调用我的Intent Service?我尝试了以下代码,但没有起作用...

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 这个类的清单文件中定义了什么? - Ali Imran
是的,清单文件没问题。我尝试将 vogella 教程中的 IntentService 转换为每 x 秒重复一次的服务。 - antkan
你没有试过@Zzokk的解决方案吗?在我看来它看起来不错。 - Ali Imran
4个回答

3

试试这个:

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);

1
在你的代码中,你没有将数据和额外信息传递给你的Service。你正在向一个Intent对象添加数据和额外信息,然后将不同的Intent对象传递给startService()方法。使用@Zzokk在另一篇帖子中提出的解决方案,它应该可以解决问题。

0

当你的广播被接收时,你的广播活动将被调用。

此时,您可以根据需要调用任何活动。

请参见下面的代码:

 @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 是被调用的活动。如果你想在消息被广播时调用该活动,就要做这个。在那时,你可以按照自己的意愿启动相应的服务。
希望你明白我的意思。

你说得对。我是新的安卓开发者。 如何将它转换为适合我的工作?我非常感激您的帮助... - antkan
@antkan:我认为你应该参考这个链接:http://www.vogella.com/articles/AndroidNotifications/article.html - Shreyash Mahajan

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接