【问题标题】:Alarmmanager not working after phone reboot手机重启后报警管理器不工作
【发布时间】:2016-12-17 10:01:46
【问题描述】:

我在按钮单击时创建了警报管理器。但是手机重启后就不行了。我的 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>

【问题讨论】:

标签: android broadcastreceiver alarmmanager android-reboot


【解决方案1】:

手机重启时所有警报都会重置,所以像这样在重启时创建回调

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

【讨论】:

  • 如何重置警报,我想停止基于 sharedpreferences 值的进度条,所以我如何更改它们,我将布尔值放入偏好中。我有近 32 个不同的进度条。所以请帮助喵喵
  • 只要通过这个接收器调用你设置闹钟的方法,简单! .重置警报后,您的代码可以像以前一样运行。对于您的共享首选项设置和重置,请阅读有关它的教程和文档
  • 你能提供小演示吗,因为我在按钮点击中添加了警报设置,所以请帮助我
  • 我尝试创建一个广播接收器,它会在设备重启时调用,然后清除警报并停止进度条,但这需要时间,例如设备启动后至少 12 秒。
  • 是的,需要这个时间,因为所有具有相同接收器的应用程序都会被一一调用,因此您的应用程序需要多少时间取决于您的应用程序何时被调用
【解决方案2】:

Ak9637 的完美答案 但别忘了添加

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多