重新启动安卓后无法恢复警报



我需要在重新启动后恢复警报。我在重新启动之前在共享首选项中保存了闹钟时间,重新启动后我正在设置该时间的闹钟,但它根本没有触发。这是我的助手类中的警报代码:

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());
    }
}

安卓清单代码:

<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>

我在日志中重新启动后得到了相同的时间,这意味着我的引导状态侦听器类代码也在工作,但它根本不会触发警报。

将此添加到您的活动中 从 u 呼叫阿拉拉姆

    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);

你能在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);

相关内容

最新更新