-2

I want to make my service always working but in the normal service if the user close the phone and open it or restart it the service is stoping can you help me. thanks

  • 1
    You can use BroadcastReceiver to do this (BOOT_COMPLETED receiver) http://stackoverflow.com/questions/5290141/android-broadcastreceiver-on-startup – xxx Jul 12 '16 at 16:42
  • 1
    Consider user commas (",") and periods (".") to be clear on your questions. – JrBenito Jul 12 '16 at 16:47

1 Answers1

0

You could use a Broadcast Receiver that would take the permission to broadcast a message on the restart of the phone that would tell the service to be started or as we call it in technical terms we would use an intent-filter having the action of starting the service when in the actions (or the ) the boot process is completed.

In manifest file :-

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

In application tag of manifest.xml :-

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

In MyBroadcastReceiver.java :-

package com.example;

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}

Now, at the end the service class of MyService would be started by this boradcasting.

Tanmayj
  • 3
  • 2