i am building a small widget for learning purpose, it simply has an configuration activity where i set the update interval. it works normally and i can create multiple instance of it. but when i reboot the phone the alarm manager stops, and the widget won't update. after some search and google'ng i learned that i have to add a BOOT COMPLETE receiver but after several attempts i failed to implement so any one has an i idea about how to add that or any good source code example on widgets.
Asked
Active
Viewed 5,113 times
4
-
define "failed to implement". http://whathaveyoutried.com ? – njzk2 Nov 22 '12 at 10:52
-
what you mean by `i failed to implement` please elaborate... – Praful Bhatnagar Nov 22 '12 at 10:52
-
i tried to add boot complet receiver similar to what warpitz answered but it didn't work... i don't have much time to fully understand the alarm manager concept as am short on time so tried few codes that i got from searches but non worked – user1844755 Nov 22 '12 at 17:55
-
@user1844755 I had forgotten about a permission in the manifest, that has been added so it should work properly now. – Warpzit May 14 '13 at 07:25
2 Answers
7
To do something at boot you simply do following.
First in the manifest
, this is added under 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>
In order for this to work you need to add permission to receive the Broadcast in the manifest with following line:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Then you have a class BootSetter:
public class BootSetter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do your stuff
}
}
There is a similar post, though not completly the same here. It's about running an alarm every day at noon.
-
it is the stuff in the onRecieve that am having problem with.... what code i need to put in there to reactivate the alarm for all instances – user1844755 Nov 22 '12 at 16:51
-
@user1844755 You need to save the alarms in sharedpreference or some other persistent storage and then set them again, yes each and everyone of them. Just like you set them the first time. – Warpzit Nov 22 '12 at 18:29
0
I think you are setting alarm manager in class other then AppWidgetProvider
extended class(widget class) .Better you should set an alarmmanager in OnUpdate method AppWidgetProvider
extended class (widget class)then there will be no need of setting the alarm again after boot.

Shakeeb Ayaz
- 6,200
- 6
- 45
- 64