Ne peut pas garder android vivant après l'application est fermée

Je suis en train de frayer un service qui reste actif tout le temps, même si l'utilisateur ferme l'application. Selon ces fils

Gardez le service de localisation vivant lorsque l'application est fermée

Android Service S'Arrête Quand L'Application Est Fermée

Android: maintenir les Services en cours d'exécution lorsque l'app est tué

ceci peut être accompli avec IntentServices ou d'un Service.START_STICKY

Pourtant, j'ai essayé les deux types de services, sans succès. En d'autres mots, mes services sont tués lorsque l'application est fermée par l'utilisateur. Quelqu'un peut-il point si ce n'est peut être fait et comment le faire? Voici ce que j'ai essayé sans succès:

Avec IntentService:

public class MyIntentService extends IntentService {
    private final int mPollingTimeMS = 500;
    private int mInitializationPollingCount = 0;
    private Thread mPollThread;
    public MyIntentService() {
        super("MyIntentService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        mPollThread = new Thread() {
            public void run() {
                while (true) {
                    try {
                        Log.e(Constants.Engine.LOGGER_TAG_DEV,
                                "SDK Service Running: " +
                                        mInitializationPollingCount * mPollingTimeMS +
                                        "ms have elapsed");
                        mInitializationPollingCount++;
                        sleep(mPollingTimeMS);

                    } catch (Exception e) {
                        StackTraceElement trace = new Exception().getStackTrace()[0];
                        Logger.e(Constants.Engine.LOGGER_TAG_APP, "[Exception:" + e.toString() + "]" +
                                trace.getClassName() + "->" + trace.getMethodName() + ":" + trace.getLineNumber());
                    }
                }
            }
        };
        mPollThread.start();
    }
}

et avec des Services:

public class MyService extends Service {
    public MyService() {
    }
    private final int mPollingTimeMS = 500;
    private int mInitializationPollingCount = 0;
    private Thread mPollThread;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mPollThread = new Thread() {
            public void run() {
                while (true) {
                    try {
                        Log.e(Constants.Engine.LOGGER_TAG_DEV,
                                "SDK Service Running: " +
                                        mInitializationPollingCount * mPollingTimeMS +
                                        "ms have elapsed");
                        mInitializationPollingCount++;
                        sleep(mPollingTimeMS);

                    } catch (Exception e) {
                        StackTraceElement trace = new Exception().getStackTrace()[0];
                        Logger.e(Constants.Engine.LOGGER_TAG_APP, "[Exception:" + e.toString() + "]" +
                                trace.getClassName() + "->" + trace.getMethodName() + ":" + trace.getLineNumber());
                    }
                }
            }
        };
        mPollThread.start();
        return Service.START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        //I tried to return null here, but this
        //service gets killed no matter what.
        return null;
    }
}

et voici le manifeste:

    <service
        android:name=".mycompany.MyService"
        android:enabled="true"
        android:exported="true"
        android:process=":process1">
    </service>
    <service
        android:name=".mycompany.MyIntentService"
        android:process=":process2"
        android:exported="false">
    </service>

Je vais ajouter que je suis la fermeture de l'appli de test pas avec un bouton de fermeture, mais en utilisant le système d'exploitation Android app manager. Voir l'image ci-dessous

Ne peut pas garder android vivant après l'application est fermée

Enfin, le pilote de l'activité (pas beaucoup là-bas)

public class MainActivity extends AppCompatActivity {

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

        Intent intent1 = new Intent(getBaseContext(), MyService.class);
        startService(intent1);
        Intent intent2 = new Intent(getBaseContext(), MyIntentService.class);
        startService(intent2);

    }
}

J'ai aussi essayer d'ajouter un avis et d'en faire un de premier plan de services, mais toujours la même chose. Le moment où j'ai fermer l'application, tout se fait tuer. C'est ce que j'ai ajouté:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    showNotification();
...etc..

private void showNotification() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    int iconId = R.mipmap.ic_launcher;
    int uniqueCode = new Random().nextInt(Integer.MAX_VALUE);
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(iconId)
            .setContentText("Context Text")
            .setContentIntent(pendingIntent).build();
    startForeground(uniqueCode, notification);
}
  • Parfois, si vous êtes faible sur la mémoire, le service s'arrête indépendamment. Essayez de faire comme un de premier plan de services si vous en avez besoin persistant, vous pouvez le faire par une notification et d'appeler le service. Beaucoup de documentation autour de.
  • Je doute que ce soit le problème. Le téléphone fonctionne avec une seule application, et ça s'arrête les services.
  • vous ne voulez pas un intentservice pour cette
InformationsquelleAutor gmmo | 2017-02-09