phone 重启后 Alarmmanager 不工作

Alarmmanager not working after phone reboot

我在单击按钮时创建了 alarmmanager。但在 phone 重新启动后它不起作用。我的 AlarmbroadcastReceiver 在 phone 重启时不调用。它在 phone 锁定时工作,应用程序被杀死但在 phone 重新启动后不工作 我创建了一个进度条,它在单击按钮时启动并在警报广播触发后停止,但在 phone 重新启动时它不会停止。我添加了我的按钮点击事件和广播接收器 class

按钮点击事件

b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
pb1.setVisibility(View.VISIBLE);
progress_edit.putBoolean("progress_one", true);
progress_edit.apply();
                    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    Intent intnt = new Intent(getApplicationContext(), AlarmbroadcastReceiver.class);
                    intnt.setAction("com.ex.Alarm");
                    PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
                    manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
                                Log.d("Broadcast ","Fired");
                }
            });

广播接收器Class

    @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("inside","broadcast receive");
            if(intent.getAction().equalsIgnoreCase("com.ex.Alarm"))
            {
enterSys_progress_edit.putBoolean("progress_one", false);
                enterSys_progress_edit.apply();
                Toast.makeText(context,"Receive",Toast.LENGTH_LONG).show();
            }

        }

我的清单文件

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.krutarth.alarm">
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".AlarmbroadcastReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>

当phone重启时所有的警报都被重置,所以像这样在重启时创建一个回调

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
       ////// reset your alrarms here 
    }

}

}

还要将此添加到您的清单中以注册广播

<receiver android:name=".receiver.BootCompletedIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>

创建一个方法说,

setMyalarm(){
     // here set the alarm as u need
}

现在调用这个方法 setMyAlarm ,无论何时何地你需要设置那个特定的警报,无论是点击按钮还是重启接收器

Ak9637 完美回答 但没有忘记添加

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