I want开发者_如何学编程 to set an alarm via AlarmManager
class. Normally, It works fine and everything is OK.
But if my application process stops or Phone turns off and turns on again, The alarm wont start. Any help?
or other service that android provides?
thanks
Android will erase all intent of alarm manager if you will restart your phone.
You have to create a receiver for start BOOT_COMPLETED
and then you will get onReceive() method when your device will start.In this method you can create all alarm again.
You have to declare receiver in manifest
<receiver android:name=".MyStartupIntentReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And your receiver will be
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent1) {
// You can update pending intent here
}
}
For startup, try http://www.anddev.org/launch_activity_on_system-emulator_startup-t428.html and for your process stop, try Android: How to auto-restart application after it's been "force closed"?
精彩评论