广播服务没有被调用

时间:2013-01-11 19:32:59

标签: java android eclipse

使用以下代码

 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永远不会被调用。我做错了什么 ?

1 个答案:

答案 0 :(得分:1)

您需要在AndroidManifest.xml中正确定义它:

<receiver 
android:process=":remote"
android:name=".BootUpReceiverRecall" />

看看“android:name”标签,
你需要在“BootUpReceiverRecall”之前添加一个点(“。”),如果它与你的应用程序在同一个包中,但如果它不是你可以简单地使用全名,如“app.package.receivers.BootUpReceiverRecall”。