开发者

Android AlarmManager did not work after process stops

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"?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜