No se permite iniciar el servicio de Intención - Android Oreo

Marcus Smith:

Actualmente estoy usando la función startWakefulService que está fallando en Oreo. Me di cuenta que tampoco debe de cambiar a startForegroundService () y utilizar un servicio de primer plano, o cambie a JobIntentService pero según mi código de abajo no estoy seguro de qué hacer. (Lo siento, soy un novato androide). Cualquier punto en la dirección correcta será apreciado considerablemente.

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GCMIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));

    setResultCode(Activity.RESULT_OK);
}
}

Este es el error actual consigo cuando se ejecuta en Android 8.x

Excepción grave: java.lang.RuntimeException No se puede iniciar el receptor com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver: java.lang.IllegalStateException: No se permite iniciar el servicio Intención {actuar = com.google.android.c2dm.intent.RECEIVE flg = 0x1000010 PKG = com.app.app cmp = com.app.app / .gcm.GCMIntentService (tiene extras)}: aplicación está en segundo plano UID UidRecord {f602fba u0a114 RCVR procsos de inactividad: 1 ss (0,0,0)}

Abdulmoiz Esmail:

Experimento la misma conducta.

¿Por qué ocurre este problema?

Debido a los nuevos límites de ejecución en segundo plano de Android 8 y usted no debe comenzar a fondo los servicios.

¿Cómo me fijo

Migrar el GCMIntetServiceque JobIntentServiceen lugar de IntentService.

Por favor seguir los siguientes pasos: 1) Añadir el permiso BIND_JOB_SERVICE a su servicio:

<service android:name=".service.GCMIntentService"
        android:exported="false"
        android:permission="android.permission.BIND_JOB_SERVICE"/>

2) En su GCMIntentServicelugar se amplía el IntentService, uso android.support.v4.app.JobIntentServicey anular onHandleWork luego retire el overrideen ti onHandleIntent.

public class GCMIntentService extends JobIntentService {

    // Service unique ID
    static final int SERVICE_JOB_ID = 50;

    // Enqueuing work in to this service.
    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, GCMIntentService.class, SERVICE_JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        onHandleIntent(intent);
    }

    private void onHandleIntent(Intent intent) {
        //Handling of notification goes here
    }
}

Entonces, finalmente, en su GCMBroadcastReceiver, poner en cola el GCMIntentService.

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        // startWakefulService(context, (intent.setComponent(comp)));

        //setResultCode(Activity.RESULT_OK);

        GCMIntentService.enqueueWork(context, (intent.setComponent(comp)));
    }
}

Este trabajo de implementación para mí después de que actualizar nuestro SDK objetivo a 27 y espero que funcione para usted.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=182609&siteId=1
Recomendado
Clasificación