[英]Broadcast service not getting called
使用以下代碼
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永遠不會被調用。 我在做什么錯?
您需要在AndroidManifest.xml中正確定義它:
<receiver
android:process=":remote"
android:name=".BootUpReceiverRecall" />
看一下“ android:name”標簽,
如果與應用程序位於同一軟件包中,則需要在“ BootUpReceiverRecall”之前添加點(“。”)。如果不是,則可以簡單地使用全名,例如“ app.package.receivers.BootUpReceiverRecall”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.