Farid Valiyev
Farid Valiyev

Reputation: 203

How can I create Android service all time working

I want create Android service all time working to when I stopped it. Restart device, Garbage collector and etc don't affect it's working. Advice me best example please.

Upvotes: 3

Views: 808

Answers (3)

Vaibhav Arora
Vaibhav Arora

Reputation: 419

For this you will have to set a BroadcastReciver which will receive boot completed. in that receiver start the service you want following is exact code.

Service class

public class BroadCastService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.tone);
        mediaPlayer.start();
        return Service.START_STICKY;
    }
}

BroadCastReciver class

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

inside Manifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
// add following in <application>
<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Upvotes: 1

Khang .NT
Khang .NT

Reputation: 1579

  • You must return START_STICKY in method onStartCommand() (or just create Foreground Service with a notification in status bar, it will run all time even if low memory, eg: music player use forceground service to make sure the music always plays)
  • After device reboot, you must broastcat this event and start your service.
  • In non root device, your service can be stopped by an orther app (eg: Cleanmaster), but if you return START_STICKY, it will auto restart. With non root device, your service will be stopped when user Force stop your application (in App manager setting). To prevent user force stop your application, you can request user grant Device Administration permission with your app.
  • In rooted device, cannot prevent CleanMaster Force stop your app even if you return START_STICKY or granted Device Administration because this app maybe have granted SuperUser permission.

Upvotes: 2

David Wasser
David Wasser

Reputation: 95578

You need to have a BroadcastReceiver that listens for the BOOT_COMPLETED broadcast Intent and start your Service when the device boots.

Your Service should return START_STICKY from onStartCommand(). This will restart your Service if Android decides to kill it for any reason.

You cannot prevent Android from killing your Service if it wants to. All you can do is try to make sure that your Service gets restarted if killed.

Also, if your user force stops your application, your Service will be stopped and will not be automatically started again. In this case, the user will need to launch your app again in order for you to restart your Service.

Upvotes: 3

Related Questions