尽管该应用已被终止,但服务仍在运行

时间:2018-07-27 08:25:48

标签: android service android-service

我试图了解onStartCommand()中服务的bwtettn STICKY和NOT_STICKY意图的区别。 我创建了下面代码所示的MainActivity和Service。我预期当我按下按钮mBtnStopApp时,将调用onDestroy()然后 我应该收到此日志** Log.w(TAG,SubTag.bullet(“ ++++++++服务未运行++++++++”))); ** 因为onStartCommand()返回NOT_STICKY intnet,这意味着当应用程序被终止时,服务也应停止 但这永远不会发生。

发生的事情是,当我按下按钮mBtnStopApp时,该应用程序完成了,但是我收到以下日志: ** Log.w(TAG,SubTag.bullet(“ ++++++++服务正在运行++++++++”))); **

请让我知道为什么尽管返回START_NOT_STICKY并且该应用被终止,该服务仍在运行

MainActivity

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private TextView mTv = null;
private Button mBtnStartStickyService = null;
private Button mBtnStartNonStickyService = null;
private Button mBtnStopApp = null;
private Button mBtnStopNonStickyService = null;
private Button mBtnStopStickyService = null;
private ServicesUtils mServiceUtils = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.w(TAG, SubTag.bullet("@onCreate"));

    this.mServiceUtils = new ServicesUtils(this);
    this.mBtnStartNonStickyService = (Button) findViewById(R.id.btnNonStickyService);
    this.mBtnStartStickyService = (Button) findViewById(R.id.btnStickyService);
    this.mBtnStopApp = (Button) findViewById(R.id.btnStopApp);
    this.mBtnStopNonStickyService = (Button) findViewById(R.id.btnStopNonStickyService);
    this.mBtnStopStickyService = (Button) findViewById(R.id.btnStopStickyService);
    this.mTv = (TextView) findViewById(R.id.tv);

    this.mBtnStartNonStickyService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ComponentName componentName = startService(setRunningStateForService(NonStickyService.class, "start"));
            Log.i(TAG, "@mBtnStartNonStickyService: componentName: " + componentName);
        }
    });

    this.mBtnStopNonStickyService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ComponentName componentName = startService(setRunningStateForService(NonStickyService.class, "stop"));
            Log.i(TAG, "@mBtnStopNonStickyService: componentName: " + componentName);
        }
    });
    this.mBtnStopApp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

private Intent setRunningStateForService(Class<NonStickyService> serviceClass, String state) {
    Log.w(TAG, SubTag.bullet("@setRunningStateForService"));
    Intent intent = new Intent(this, serviceClass);
    intent.putExtra("running", state);
    return  intent;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.w(TAG, SubTag.bullet("@onDestroy"));
    if (this.isNonStickyServiceRunning()) {
        Log.w(TAG, SubTag.bullet("++++++++ SERVICE IS RUNNING ++++++++"));
    } else {
        Log.w(TAG, SubTag.bullet("++++++++ SERVICE IS NOT RUNNING ++++++++"));
    }
}

private boolean isNonStickyServiceRunning() {
    ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (NonStickyService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
}

代码

public class NonStickyService extends Service {

private static final String TAG = NonStickyService.class.getSimpleName();
private HeavyWorkRunnable mHeavyWorkRunnable = null;
private String running = null;

@Override
public void onCreate() {
    super.onCreate();
    Log.w(TAG, SubTag.bullet("@onCreate"));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w(TAG, SubTag.bullet("@onStartCommand"));

    running = intent.getStringExtra("running");
    if (running != null && !running.isEmpty() && running.equals("start")) {
        this.StartHeavyWorkThread();
    }
    return Service.START_NOT_STICKY;
}

private void StartHeavyWorkThread() {
    Log.w(TAG, SubTag.bullet("@StartHeavyWorkThread"));
    this.mHeavyWorkRunnable = new HeavyWorkRunnable();
    new Thread(this.mHeavyWorkRunnable).start();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    Log.w(TAG, SubTag.bullet("@onBind"));
    return null;
}

@Override
public void onDestroy() {
    Log.w(TAG, SubTag.bullet("@onDestroy"));
    super.onDestroy();
    this.running = "stop";
}

class HeavyWorkRunnable implements Runnable {
    int counter = 0;
    private Bundle b = null;
    public void run() {
        Log.w(TAG,SubTag.bullet("@HeavyWorkRunnable"));

        while(running.equals("start")) {
            b = new Bundle();
            b.putInt("what", ++counter);
            Log.w(TAG,SubTag.bullet("@HeavyWorkRunnable: counter: " + counter));
            Log.w(TAG,SubTag.bullet("@HeavyWorkRunnable: b.getInt('what'): " + b.getInt("what")));

            Message msg = handler.obtainMessage();
            msg.setData(b);
            handler.sendMessage(msg);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Log.i(TAG,SubTag.bullet("NonStickyService will Stop"));
        stopSelf();
    }
}

android.os.Handler handler = new android.os.Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        int what = msg.getData().getInt("what");
        Log.w(TAG,SubTag.bullet("@handler: what: " + what));
    }
};
}

0 个答案:

没有答案
相关问题