Broadcast service 没有被调用

我使用下面的代码调用 Broadcast service

Intent i = new Intent(this, BootUpReceiverRecall.class);
        sendBroadcast(i);
 <receiver  android:process=":remote" android:name="BootUpReceiverRecall"></receiver>
public class BootUpReceiverRecall extends BroadcastReceiver 
{
      // Restart service every 30 seconds
      private static final long REPEAT_TIME = 1000 * 30;
      @Override
      public void onReceive(Context context, Intent intent) 
      {
        AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, BootUpReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();
        // Start 30 seconds after boot completed
        cal.add(Calendar.SECOND, 30);
        //
        // Fetch every 30 seconds
        // InexactRepeating allows Android to optimize the energy consumption
        service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), REPEAT_TIME, pending);
        // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
        // REPEAT_TIME, pending);
      }

但是我的BootUpReceiver 从来没有被调用,哪里出错了呢?


肥皂起泡泡
浏览 429回答 1
1回答

holdtom

Intent&nbsp;i&nbsp;=&nbsp;new&nbsp;Intent(this,&nbsp;BootUpReceiverRecall.class); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendBroadcast(i);为什么要这样写呢? 我是没这样写过广播分2中注册方式:1中是代码注册,局部广播 可以写在Activity中YourReceiver&nbsp;reveiver&nbsp;=&nbsp;new&nbsp;YourReceiver&nbsp;(); IntentFilter&nbsp;filter&nbsp;=&nbsp;new&nbsp;IntentFilter(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filter.addAction("yourAction"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;registerReceiver(reveiver&nbsp;,&nbsp;filter);Activity生命周期中可以管理register 和 unregister2.manifest配置 节点下&nbsp;创建 YourReceiver在xx.xx.xx.receiver(随便写)包下 extends BrocastReceiver根据action过滤你这个可改成Intent&nbsp;i&nbsp;=&nbsp;new&nbsp;Intent(“action”); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sendBroadcast(i);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java