重启后Android安全警报重置不起作用

时间:2014-04-26 10:27:38

标签: android alarmmanager alarm android-alarms android-bootstrap

我有一个名为 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);

1 个答案:

答案 0 :(得分:0)

<强>清单    只写两个接收器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
}
}