手机重启后,Alarmmanager无法正常工作

时间:2016-12-17 10:01:46

标签: android broadcastreceiver alarmmanager android-reboot

我在按钮点击时创建了alarmmanager。但手机重启后无法正常工作。我的AlarmbroadcastReceiver没有拨打电话重启。当手机锁定,应用程序被杀,但手机重启后无法正常工作时,它已经创建了一个进度条,它在按钮点击时启动并在警报广播被触发后停止,但在手机重启时不会停止。我添加了我的按钮点击事件和广播接收器类

按钮点击事件

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

BroadcastReceiver类

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

2 个答案:

答案 0 :(得分:3)

手机重启时会重置所有闹钟,因此请在重新启动时创建一个回调

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,无论何时何地需要设置该特定警报,无论是单击按钮还是重启接收器

答案 1 :(得分:0)

完美的答案由Ak9637

但是没有忘记添加
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>