今天被stopself坑了,mark一下。。。
一直知道stopself是停掉Service的方法,但是却不知道什么时候停止。以为调用了stopself就会马上停止,实际上我错了。
在onStartCommond方法里面调用stopself方法时,不会马上停止,而是onStartCommond方法执行结束才会停止。
还有一点,调用stopself方法之后,service会执行onDestory方法。
另外,如果onStartCommond中启动一个线程,调用stopself,线程也不会被杀死。
写了一个简单的测试程序,代码如下:
package com.example.teststopself;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("tttt", "onStartCommand");
stopSelf();
Log.d("tttt", "after stop self");
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(true) {
i++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("tttt", "i="+i+"");
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d("tttt", "onDestory");
super.onDestroy();
}
@Override
public void onCreate() {
Log.d("tttt", "onCreate");
super.onCreate();
}
}
最后输出的log,如下:
07-07 00:00:14.691: D/tttt(8404): onCreate
07-07 00:00:14.691: D/tttt(8404): onStartCommand
07-07 00:00:14.696: D/tttt(8404): after stop self
07-07 00:00:14.836: D/tttt(8404): onDestory
07-07 00:00:16.696: D/tttt(8404): i=1
07-07 00:00:18.696: D/tttt(8404): i=2
07-07 00:00:20.696: D/tttt(8404): i=3
07-07 00:00:22.701: D/tttt(8404): i=4
07-07 00:00:24.701: D/tttt(8404): i=5
07-07 00:00:26.701: D/tttt(8404): i=6
07-07 00:00:28.701: D/tttt(8404): i=7
07-07 00:00:30.701: D/tttt(8404): i=8
07-07 00:00:32.706: D/tttt(8404): i=9
07-07 00:00:34.706: D/tttt(8404): i=10
07-07 00:00:36.706: D/tttt(8404): i=11
07-07 00:00:38.706: D/tttt(8404): i=12
07-07 00:00:40.706: D/tttt(8404): i=13
07-07 00:00:42.706: D/tttt(8404): i=14
07-07 00:00:44.706: D/tttt(8404): i=15
07-07 00:00:46.711: D/tttt(8404): i=16
07-07 00:00:48.711: D/tttt(8404): i=17
07-07 00:00:50.711: D/tttt(8404): i=18
07-07 00:00:52.711: D/tttt(8404): i=19
由此,让我想起了Activity的finish方法,finish方法也可能不会马上停掉Activity,测试了一下,果然时这样。代码如下:
package com.example.teststopself;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("ttt", "oncreate");
finish();
Log.d("ttt", "after finish");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop() {
Log.d("tttt", "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("tttt", "onDestroy");
}
@Override
protected void onPause() {
super.onPause();
Log.d("tttt", "onPause");
}
@Override
public void finish() {
super.finish();
Log.d("tttt", "finish");
}
}
打印的log如下:
07-07 00:06:23.871: D/ttt(8717): oncreate
07-07 00:06:24.011: D/tttt(8717): finish
07-07 00:06:24.011: D/ttt(8717): after finish
07-07 00:06:24.161: D/tttt(8717): onDestroy
当调用finish方法时,onCreate方法会继续执行,之后调用onDestory方法。
最后,总结一下,Service的stopself方法的功能是,当完成所有功能之后,将service停掉,而不是等着系统回收。同样finish方法,是当系统执行完onCreate方法之后,调用onDestory方法销毁Activity。