0

In my app I am restarting my service from activity every one minute. but if application is force closed will it start the service. What is the state of activity that time.

This is my code.

void toggleLogging(boolean isStart, int interval) 
{
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent loggerIntent = PendingIntent.getBroadcast(this, 0,new Intent(this,AlarmReceiver.class), 0);       
    long duration = interval * 60 * 1000;
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime(), duration, loggerIntent);
    AppSettings.setServiceRunning(this, true);
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Manasi
  • 348
  • 1
  • 4
  • 14

1 Answers1

0

Yes alarm are registered even app is force closed, So no need to worry

As per your request I have added the code

public class SampleActivity extends Activity{
    public static AlarmManager mAlarmManager;
    public static PendingIntent mPintent;
    public static Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Context context = getApplicationContext();
        SampleActivity.startAlarmManager(context);
    }

    public static void startAlarmManager(Context context){
        SampleActivity.mIntent = new Intent(context, YourService.class);
        mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        mPintent = PendingIntent.getService(context, 0, SampleActivity.mIntent, 0);
        Calendar cal = Calendar.getInstance();
        mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2000, mPintent);
    }
}

Then start the same alarm at boot complete

public class SampleBootComplete extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent arg1) {
        SampleActivity.startAlarmManager(context);
    }

}

Register boot complete receiver in manifestfile

<receiver android:name="com.sample.receiver.SampleBootComplete">
           <intent-filter >
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
           </intent-filter>
       </receiver>
Naga
  • 1,931
  • 4
  • 25
  • 42
  • 2
    But they are cleared on reboot. You need to request permission to receive the ON_BOOT_COMPLETE intent from the system when a boot is completed and then register the alarm again when you receive the intent if you want to have the contents of your PendingIntent continue to be broadcasted. – dcow Apr 15 '13 at 06:25
  • Yes exactly she has to handle the thing on boot complete receiver, I am agree with you David, I am accepting your answer – Naga Apr 15 '13 at 06:26
  • but when I do Settings > Manage Applications > Force close. It is not restarting my service. I am little confused. – Manasi Apr 15 '13 at 06:34
  • The `AlarmManager` has nothing to do with your service. It is part of the operating system (well if you want to call Android that) and is not affected by the lifecycle of your individual application. – dcow Apr 15 '13 at 06:42
  • Can you your service code because alarm implementation code looks fine – Naga Apr 15 '13 at 06:45
  • ok. then why is it not restarting my service after the given interval if it is force closed. I am writing the above code in my main Activity. – Manasi Apr 15 '13 at 06:46
  • Actually Narendra sir I am starting my alarm manager from Activity. Can this b the reason for my problem. – Manasi Apr 15 '13 at 06:48
  • No it is not you are starting the alarm manager from activity is just fine but you should start the same alarm manager from boot complete receiver, As per my suggestion you should use static variable .. If you want I can provide you code – Naga Apr 15 '13 at 07:17
  • I read this on SO: if your receivers are registered in AndroidManifest then yes, your app will still receive it. On the other hand, if you are registering via code (in service/activity), then the app won't receive it – Manasi Apr 15 '13 at 08:15
  • I have edited my answer and added code ..have a look and let me know – Naga Apr 15 '13 at 08:23
  • Am I not doing the same thing.And why to register with boouUp register. I want to start service if force closed – Manasi Apr 15 '13 at 08:27
  • Because once phone is rebooted then alarm manager will get unregistered so thats why you should register the alarm to start the service – Naga Apr 15 '13 at 08:32
  • Not restarting. I also tried but no luck! target and min sdk both are 18. – Suresh Sharma Nov 19 '14 at 10:31