4

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

MHM
  • 854
  • 1
  • 9
  • 17
  • It is logical that if the process of your class dies, the alarm dies with it. If you want it to be working after such events, you'll have to save the state of your app when it's killed or phone restarted, relaunch your app and get the alarm config. – Sephy Sep 29 '11 at 12:37
  • In fact I want something like Android Built in Alarm Clock which even turns phone on If it is not. – MHM Sep 29 '11 at 13:25
  • 3
    @Sephy: "It is logical that if the process of your class dies, the alarm dies with it." -- this is incorrect. – CommonsWare Sep 29 '11 at 13:45

2 Answers2

7

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 

    }
}
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • 2
    Thanks for quick reply. But is there any better way? Will Android Built In clock alarm do the same? – MHM Sep 29 '11 at 12:49
  • I had searched more to get solution of this problem but I did not get any better way than this.I had implement this logic in one of my application. – Dharmendra Sep 29 '11 at 13:56
2

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

Community
  • 1
  • 1
Xavjer
  • 8,838
  • 2
  • 22
  • 42