0

I want to start my Android app automatically on following cases :

  1. Reboot
  2. App were stoped throug TaskManager or Ram clearing

I search on web and found stuff about Android-Services. I implement some code for start my service on reboot, but i doesn't work, and i have no idea how to get informed about case 2.

Can you give me an advice?

This is my code for Service:

on manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 


<service android:name="TimetableService">
 <intent-filter>
  <action android:name=".TimetableService" />
 </intent-filter>
</service>

<receiver android:enabled="true" android:name=".OnBootReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

OnBootReceiver :

public void onReceive(Context context, Intent intent) 
{
    Intent serviceIntent = new Intent();
    serviceIntent.setAction("TimetableService");
    context.startService(serviceIntent);
}

TimetableService :

 public class TimetableService extends Service 
 {

@Override
public IBinder onBind(Intent intent) {

    return null;

}

@Override
public void onCreate() {
    super.onCreate();

    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    super.onDestroy();

    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

}

}

Kooki
  • 1,157
  • 9
  • 30
  • 1
    [Check this one and get it working](http://stackoverflow.com/questions/7690350/android-start-service-on-boot/7690600#7690600) – Lalit Poptani May 22 '12 at 13:18
  • 1
    Generally you shouldn't start after a TaskManager stops your service. That's being a bad android citizen. – CodeFusionMobile May 22 '12 at 13:19
  • @ CodeFusionMobile : ok, but how should be sure, that my app works if it not runs every time, because i have to check every day (or what user have choose) if i have to send a notification – Kooki May 22 '12 at 13:23

2 Answers2

0

I would look into using the AlarmManager to schedule your app to run at a specific time of the day. That way it doesn't matter if something closes your app down, the alarm manager will wake it up again with an intent at the time you specify.

Ruddles
  • 193
  • 2
  • 11
0

Lets use service to auto start app

Now when boot completes you can use the following code to restart your service.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyStartupIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
 // check for interreupted flag in shared pref
 // if true restart your service
}
}

Add following code to your manifest

<receiver android:name="MyStartupIntentReceiver">
<intent-filter>    
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30