Restore alarm manager after phone reboot

I create a small widget for the purpose of training, it just has configuration activity, where I set the update interval. It works fine, and I can create multiple instances. but when I restart the phone, the alarm manager stops and the widget will not update. after some searching and google'ng, I found out that I need to add a BOOT COMPLETE receiver but after several attempts that I could not implement, anyone has an idea on how to add this or any good example source code in widgets.

+4
source share
2 answers

To do something at boot, you simply follow these steps.

First in manifest , this is added to the application tag:

  <receiver android:name="AlarmReceiver"> <intent-filter> <action android:name="packagename.ACTION"/> <action android:name="packagename.ACTION2"/> </intent-filter> </receiver> <receiver android:name="BootSetter" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 

To do this, you need to add permission to receive broadcast in the manifest with the following line:

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

Then you have the BootSetter class:

 public class BootSetter extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Do your stuff } } 

There is a similar post, although not completely the same here . This means that an alarm goes off every day at noon.

+7
source

I think you are setting up the alarm manager in the class, and then the AppWidgetProvider extended class (widget class). You must set the alarm in the OnUpdate method of the AppWidgetProvider extended class (widget class), then after loading you do not need to set the alarm again.

0
source

Source: https://habr.com/ru/post/1447714/


All Articles