Keep an Android service always alive

4

I've been trying this a long time, looking for documentation and different questions in stackoverflow but I have not found the key.

I need to launch a service that does not die by closing the APP completely.

I've tried

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

with permission:

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

When launching it:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
cpuWakeLock.acquire();
Intent intent = new Intent(this, WebViewService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);

And endless codes that have not worked .. is it possible?

Any advice? Thanks!

    
asked by Pablo Cegarra 07.02.2017 в 19:05
source

2 answers

4

I have implemented something similar in my own application that what is done is to lift the service every time it dies and also when the device is restarted:

Class extending BroadcastReceiver:

public class BootBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent intent) {
         ctx.startService(new Intent(ctx, MyService.class));

    }

}

In my service:

public class MyService extends Service {

    .....

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    .....

}

In the AndroidManifest.xml:

   <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" >
    </service>

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

Edit: The service starts in the onCreate of my MainActivity:

    if (!isMyServiceRunning(MyService.class)){ //método que determina si el servicio ya está corriendo o no
        serv = new Intent(ctx,MyService.class); //serv de tipo Intent
        ctx.startService(serv); //ctx de tipo Context
        Log.d("App", "Service started");
    } else {
        Log.d("App", "Service already running");
    }
    
answered by 07.02.2017 / 19:23
source
4

According to your code I see that you link a service so what you are doing is that your Activity interacts with the service:

bindService(intent, serviceConnection, BIND_AUTO_CREATE);

In this case, you could use a Foreground Service ( Foreground Service ) , which is done using the startForeground () :

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);

startForeground(ONGOING_NOTIFICATION_ID, notification);

The option I do regularly through a BroadcastReceiver is the following, you can take this article as an example:

link

1) register the service when you start your device, with this you do not have to open the application to start:

    

<receiver android:name="ReceiverCall" >
    <intent-filter>
        <action android:name="com.stackoveflow.myservicio" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

2) Add in class BroadcastReceiver code to restart the service again.

public class ReceiverCall extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Servicio", "El servicio se detuvo");
        context.startService(new Intent(context, ServiceTest.class));;
    }

}

3) Create the Android Service class and in onDestroy() implement the service start.

  public void onDestroy() {

        Intent intent = new Intent("com.stackoveflow.myservicio");
        intent.putExtra("yourvalue", "torestore");
        sendBroadcast(intent);
    }
    
answered by 07.02.2017 в 19:56