0

我每 30 秒拨打一次警报。它没有被调用。我究竟做错了什么。

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, 30);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(), 1000 * 30, pi);

我的 OnAlarmReceiver

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, DriverService.class));
}
}

警报和服务的我的清单内容

 <service
        android:name="com.taxeeta.DriverService"
        android:enabled="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" />

    <receiver
        android:name="com.taxeeta.support.OnAlarmReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>
4

1 回答 1

0

通过将代码更改为来修复它,感谢

Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
    notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notifyintent.setAction("android.intent.action.NOTIFY");
    PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
            notifysender);
于 2013-10-10T06:59:55.183 回答