简体   繁体   中英

Android duplicating service in some devices

I'm developing a simple Chronometer app which uses a Service (this is intended to keep the chronometer running even if the app has been closed).

The chronometer logic is handled by the service and my Fragment just binds to the service to get data. My problem is that, when I start the chronometer and close the app. When I open it again, the chronometer binds to a new instance of the service, so it thinks it's not running.

This happens just in one device I tested (In emulator and 4 other devices works ok). How the heck can be happening??

Here is some code :

Fragment onStart :

public void onStart() {
    super.onStart();
    Intent intent = createServiceIntent();
    getActivity().startService(intent);
    getActivity()
        .bindService(
            intent,
            mServiceConnection,
            Context.BIND_ABOVE_CLIENT
        );

}

Service constructor, onCreate , onStart , onBind and onUnbind

public ChronometerService() {
    Log.d(Config.APP_TAG, "CREATING CHRONOMETER SERVICE INSTANCE");
}

@Override
public void onCreate()
{
    super.onCreate();
    this.mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    this.mBinder = new ChronometerBinder();
    this.mPauses = new LinkedList<>();
}

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

@Override
public IBinder onBind(Intent intent) {
    Log.d(Config.APP_TAG, "SERVICE BIND. AND WAS" + (mRunning ? "" : "N'T") + " RUNNING");
    if( ! mRunning ) {
        this.mPauseLength = 0;
        this.mStoppedAt = 0;
        this.mBase = 0;
    }
    return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
    if( ! mRunning) {
        stopForeground(true);
        stopSelf();
    }
    return true;
}

When start button is tapped in the Fragment, the following method in the service is called:

public void start() {
    if(this.mBase == 0) {
        reset();
        this.mBase = now();
    }else{
        mPauseLength += now() - mStoppedAt;
    }
    Intent i = new Intent(this, MainActivity.class);
    i.putExtra(MainActivity.CURRENT_FRAGMENT, MainActivity.CHRONOMETER_TAG);
    PendingIntent intent = PendingIntent.getActivity(this, SERVICE_ID, i, PendingIntent.FLAG_UPDATE_CURRENT);

    mNotificationBuilder = new NotificationCompat.Builder(this);
    mNotificationBuilder.setContentTitle(getResources().getString(R.string.chronometer_title))
        .setSmallIcon(R.drawable.ic_timer_white_48dp)
        .setContentIntent(intent);

    Notification n = mNotificationBuilder.build();
    startForeground(SERVICE_ID, n);
    mRunning = true;
    update();
}

In the device which the app runs ok, in the log I get:

CREATING CHRONOMETER SERVICE INSTANCE
SERVICE BIND. AND WASN'T RUNNING
(Closing the app)
SERVICE BIND. AND WAS RUNNING

In the device it doesn't work I get:

CREATING CHRONOMETER SERVICE INSTANCE
SERVICE BIND. AND WASN'T RUNNING
(Closing the app)
CREATING CHRONOMETER SERVICE INSTANCE
SERVICE BIND. AND WASN'T RUNNING

Hope you can help me.

SOLVED:

The problem wasn't my code after all. The problem was Purify

It was killing my service. After adding my app to the Whitelist, the problem was solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM