我正在搭建一個出租車預訂應用程序,我需要每20秒鐘在駕駛室的當前位置。沒有調用BroadcastReceiver的onReceiver,AlarmManager
我定義了一個AlarmManager,需要它每20秒重複一次。但它不會定期重複。相反,它在233秒後重復了自己,只是一次。我在這裏做錯了什麼?
我的主屏幕有一個內部類OnAlarmReceiver,我在主屏幕的OnCreate我打電話AlarmManager
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, OnAlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
cal.getTimeInMillis(), God.UPDATE_PENDING_INTERVAL, pi);
在主屏幕
內部類
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context);
Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG)
.show();
Log.d("Taxeeta:PullPendingRequets", "CallService Location");
context.startService(new Intent(context, PullPendingRequests.class));
}
}
我AndroidManifest文件有
<service
android:name="com.taxeeta.support.PullPendingRequests"
android:enabled="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar" />
<receiver android:name=".com.taxeeta.HomeScreen.OnAlarmReceiver" />
</application>
adb shell dumpsys alarm
com.taxeeta
51471ms running, 5248 wakeups
5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver
的adb shell dumpsys alarm | grep taxeeta
ELAPSED_WAKEUP #7: Alarm{409303b0 type 2 com.taxeeta}
operation=PendingIntent{408ba2d8: PendingIntentRecord{40887be8 com.taxeeta broadcastIntent}}
com.taxeeta
5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver
不,不起作用。 OnAlarmReceiver,onReceive也沒有被調用。 – Siddharth 2013-04-08 10:04:42
同樣在這裏alarmmanager什麼都不適合我。我已經爲此嘗試了所有建議。不知道該怎麼做相當卡住 – JPM 2016-11-08 21:39:34