12

我正在构建一个出租车预订应用程序,我每 20 秒需要一次出租车的当前位置。

我已经定义了一个 AlarmManager 并且需要它每 20 秒重复一次。但它不会定期重复。相反,它在 233 秒后重复了自己,而且只重复了一次。我在这里做错了什么?

我的 HomeScreen 有一个内部类 OnAlarmReceiver,在我的 HomeScreen 的 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);

HomeScreen 中的内部类

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
4

4 回答 4

14

为了修复它,我删除了内部类 OnAlarmReceiver 并修复了 androidmanifest.xml 文件。

    <receiver
        android:name="com.taxeeta.support.OnAlarmReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>
于 2013-04-08T15:11:26.200 回答
6

如果上面的答案对您不起作用,那么还有另一种方法可以在触发过期警报时 接收任何回调。AlarmManager您只需要检查这一点:通过IntentPendingIntent. 例如,您想在onReceive您的一个接收器上接听电话,但您实例化了一个PendingIntentvia getActivityor getService,但您的实际意思是getReceiver.

创建 的实例时PendingIntent,有多种创建方法(getService, getActivity, getReceiver, getForegroundService:

如果您想要Activity意图的接收者,那么您:

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);

如果您想要BroadcastReceiver意图的接收者:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);

如果你想要一个前台Service意图的接收者:

PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);

如果您想要Service意图的接收者:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);

另外,请确保您的意图指向正确的类。(例如,为 Activity、Service 等创建意图)。如果您像这样错误地通过,您将不会接到任何电话:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

我也在这里发布了类似的答案。

高温高压

于 2017-07-14T01:48:56.317 回答
1

这段代码对我有用,并确保您已在 android 清单文件中添加了接收者。

AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
                         PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 20 seconds after boot completed
cal.add(Calendar.SECOND, 20);
//
// Fetch every 20 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                       cal.getTimeInMillis(), 1000*20, pending);
于 2013-04-08T07:41:57.190 回答
1

上述解决方案对我不起作用。

此外,通过代码动态注册也能达到目的:

Intent intent = new Intent();
intent.setAction("android.intent.action.NOTIFY");

//Register the receiver
context.registerReceiver(new OnAlarmReceiver(),new IntentFilter());
于 2019-07-31T12:21:13.910 回答