重启android后无法恢复警报

时间:2017-03-30 06:40:59

标签: android alarmmanager alarm android-alarms

我需要在重启后恢复警报。我在重启之前在 SharedPreferences 中保存闹钟时间,重新启动后我正在设置闹钟,但它根本不会触发。 这是我的Helpers类中的警报代码:

public static void scheduleNotification(Notification notification, long delay) {

    Intent notificationIntent = new Intent(AppGlobals.getContext(), AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(AppGlobals.getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long futureInMillis = SystemClock.elapsedRealtime() + delay;
    AlarmManager alarmManager = (AlarmManager)AppGlobals.getContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
    AppGlobals.putAlarmTime(futureInMillis);
    System.out.println("Alarm time before: " + futureInMillis);
}

我正在使用上面的代码在重新启动之前在另一个类中安排警报,并且在重新启动之前它可以正常工作。

这是我的AlarmReceiver类代码:

public class AlarmReceiver extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);
    Toast.makeText(context, "Alarm!!!", Toast.LENGTH_LONG).show();
}

BootStateListener类的代码:

public class BootStateListenerService extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, AppGlobals.getAlarmTime(), pendingIntent);
        System.out.println("Alarm time after reboot: " + AppGlobals.getAlarmTime());
    }
}

AndroidManifest代码:

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

<application
    android:name=".utils.AppGlobals"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".services.LocationService" />
    <receiver android:name=".services.BootStateListenerService"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

    <receiver android:name=".receivers.AlarmReceiver" />
</application>

我在Logs重新启动后得到同样的时间,这意味着我的bootstatelistener类代码也在工作,但它根本不会发出警报。

2 个答案:

答案 0 :(得分:1)

  

在你的活动中添加这个来自你的alaram

    ComponentName receiver = new ComponentName(Activity.this, BootStateListenerService .class);
    PackageManager pm = Activity.this.getPackageManager();

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

答案 1 :(得分:0)

您可以在onReceive()

中试用此代码吗?
AlarmManager mgr=
    (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, SomeActivity.class);
PendingIntent pendingIntent =PendingIntent.getService(ctxt, 0, i, 0);

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
                 AppGlobals.getAlarmTime() + 10000, 10000, pendingIntent);