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

Android 如何从广播接收器调用IntentService?,android,broadcastreceiver,intentservice,Android,Broadcastreceiver,Intentservice,我为下载数据制作了一个意向服务,我想用广播接收和报警管理器重复。我如何调用我的意向服务?我尝试了这个,但没有工作 public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Don't panik but y

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

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扩展了活动{

@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();
  }
试试这个:

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

在代码中,您没有向
服务
传递数据和附加。您正在向
意图
对象添加数据和附加,将不同的
意图
对象传递给
startService()
方法。使用@Zzokk在另一篇文章中建议的解决方案,它应该会起作用。

当收到您的广播时,您的广播活动将被调用

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

见以下代码:

 @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);
            }
在上面的代码中,AlarmReceiveNotificationForeveryMonth.class是被调用的活动 如果要在消息广播时调用活动,则必须执行此操作。此时,您可以根据需要启动相应的服务


希望你明白我的意思。

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

你在清单中为这个类定义了什么
DownloadService
?是的清单是可以的。我试着转换vogella tutorial intentservice,每x秒重播一次。你没有尝试@Zzokk的解决方案吗?看起来不错对我来说。你是对的。我是新的android开发人员。如何将此转换为对我有效?我将非常感谢你的帮助…@antkan:我想你应该参考以下内容: