2

I have created a background service and that service is working fine in below Android 6.0 when I kill app.

Also working in Android 6.0 and above but only when I minimize app.

When I kill app in Android 6.0 and above service also kill and not get restart and also not get start on BOOT_COMPLETE.

What to DO? Can I get a simple example.

I tried this: MyService.java

public class MyService extends Service {
    private MediaPlayer player;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet started");
    }

    public A_ExampleMyService() {
        super();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
        player.setLooping(true);
        player.start();
        return START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());
        startService(restartServiceIntent);
        super.onTaskRemoved(rootIntent);
    }
}

MyActivity.java

public class MyActivity extends Activity {
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_example_activity);

        this.context  = this;

        Intent background = new Intent(MyActivity.this,MyService.class);
        context.startService(background);
    } 
}

manifest.xml

<manifest>
    <application>
        <service>
            <android:name=".MyService">
            <android:enabled="true">
            <android:exported="true"/>
     </application>
</manifest>
  • Also looking for the same. Good Question. I have to call a Web Service when user kills the app from the recent mode. I am able to call a web service inside a background service while application is in recent mode. But, unfortunately not when user kills the application. It also killing my service. Looking for the solution. – Jaimin Modi Mar 29 '19 at 13:02

0 Answers0