0

I have tried this Trying to start a service on boot on Android

Also I have checked with http://blog.vogella.com/2011/12/11/automatically-starting-services-in-android-after-booting/ yet my app wont restart on Phone startup. Here's the code.

AndroidManifest.xml

           <service android:name=".MyService">
           </service>
 <receiver android:name=".Autostart"
 android:enabled="true"
 android:exported="false">  
 <intent-filter>     
 <action android:name="android.intent.action.BOOT_COMPLETED"/>  
 </intent-filter>  
   </receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

MyService

   public class MyService extends Service implements LocationListener {



@Override
public IBinder onBind(Intent intent) {

    return mBinder;
}

private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    MyService getService() {
        return MyService.this;
    }
}

@Override
public void onCreate() {


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("LocalService", "Received start id " + startId + ": " + intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

@Override
public void onDestroy() {
    // Cancel the persistent notification.
    this.stopSelf();
    // Tell the user we stopped.

}

MainActivity

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

startService(new Intent(getBaseContext(), MyService.class));

Am I going wrong anywhere?

Edit:

    public void onReceive(Context arg0, Intent arg1) 
{
    Intent intent = new Intent(arg0,MyService.class);
    arg0.startService(intent);
    Log.i("Autostart", "started");

}

This is my BroadcastReceiver. Yet no results.

Community
  • 1
  • 1
Tejaswi Parande
  • 95
  • 1
  • 2
  • 8

2 Answers2

1

try register BroadcastReceiver for ACTION_BOOT_COMPLETED. The you can recieve startup broadcast.

public class BootBroadCast extends BroadcastReceiver{
    @Override  
    public void onReceive(Context context, Intent intent) {  

         Intent service=new Intent(context, MyService.class);  
         context.startService(service);  
    }
}

second you need add this in AndroidManifest.xml

<receiver android:name=".BootBroadCast" >  
            <intent-filter>  
                <action android:name="android.intent.action.BOOT_COMPLETED" >  
                </action>  

            </intent-filter>  
        </receiver>
whb_zju
  • 133
  • 1
  • 1
  • 10
0

I have followed the article at vogella and was able to get my service started at Boot.

First of all, you should have a BroadcastReceiver for the event android.intent.action.BOOT_COMPLETED. Just follow the steps from the blog. It seems you have started the service on your app's 'onCreate'. This will get invoked only if the user starts your app.

When the system boots up, your receiver class's onReceive() method will be invoked. Here is where you should start your activity as below.

public class BootReceiever extebds BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, YourService.class);
        context.startService(service);
    }
}

From the research that I have done, it is better to start your service after a short delay as many applications / services may start at boot, so chances of low memory are more. I started my service after a small delay.

midhunhk
  • 5,560
  • 7
  • 52
  • 83