1

我有一个名为StopAlarmReceiver的类和另一个BootHandler

BootHandler在重启重置警报,这是在启动之前设置的。我的问题是BootHandler设置了警报但没有被解雇

清单文件中的条目。

    <receiver
        android:name="com.sign.android.myscheduler.app.StopAlarmReceiver"
        android:enabled="true"
        android:exported="true" >
    </receiver>

    <receiver
        android:name="com.sign.android.myscheduler.app.BootHandler"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

BootHandler 中的代码。

AlarmManager mgr= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StopAlarmReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 1, i, 0);

long time = sharedPreferences.getLong("Old_time", 0);
Log.e(TAG, "New time: " +new Date(SystemClock.elapsedRealtime() + time));

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + time, 5000, pi);
4

1 回答 1

1

manifest 只写两个接收器 1 用于警报和 1 用于处理启动

  <receiver
        android:name="packagename.AlarmReceiver"

        >

    </receiver>

    <!-- Will not be called unless the application explicitly enables it -->
     <receiver android:name="com.avion.contact_app.DeviceBootReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

添加以下权限

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

在 DeviceBootReceiver 类中,首先检查意图操作

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences prefs = context.getSharedPreferences(
            "DefaultReminder", context.MODE_PRIVATE);

    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))

    {
//do whatever you want after reboot or fire alarm again
}
}
于 2016-02-11T10:24:07.760 回答