android Service的stopself方法

今天被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。

  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
为了处理这种后台进程,Android引入了Service的概念。ServiceAndroid中是一种长生命周期的组件,它不实现任何用户界面。最常见的例子如:媒体播放器程序,它可以在转到后台运行的时候仍然能保持播放歌曲;或者如文件下载程序,它可以在后台执行文件的下载。 让我们来看下如何创建Service: 创建一个Service Android中已经定义了一个 ‘Service’类,所有其他的Service都继承于该类。Service类中定义了一系列的生命周期相关的方法,如: onCreate(), onStart(), onDestroy()。参见下例: package com.wissen.testApp.service; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(this, “Service created…”, Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, “Service destroyed…”, Toast.LENGTH_LONG).show(); } }上例中的这个Service主要做了这些事:当服务创建和销毁时通过界面消息提示用户。 如Android中的其它部件一样, Service也会和一系列Intent关联。Service的运行入口需要在AndroidManifest.xml中进行配置,如下: 1. <service class=”.service.MyService”> 2. <intent-filter> 3. <action android:value=”com.wissen.testApp.service.MY_SERVICE” /> 4. </intent-filter> 5. </service> 复制代码 之后我们的Service就可以被其他代码所使用了。 使用Service: 应用程序可以通过调用 Context.startService方法来启动Service。如果当前没有指定的Service实例被创建,则该方法会调用 Service的onCreate方法来创建一个实例;否则调用Service的onStart方法。参见下列代码: 1. .. 2. Intent serviceIntent = new Intent(); 3. serviceIntent.setAction(”com.wissen.testApp.service.MY_SERVICE”); 4. startService(serviceIntent); 5. .. 复制代码 以上代码调用了startService方法Service会持续运行,直到调用stopService()或stopSelf()方法。 还有另一种绑定Service的方式: 1. … 2. ServiceConnection conn = new ServiceConnection() { 3. @Override 4. public void onServiceConnected(ComponentName name, IBinder service) { 5. Log.i(”INFO”, “Service bound “); 6. } 7. 8. @Override 9. public void onServiceDisconnected(ComponentName arg0) { 10. Log.i(”INFO”, “Service Unbound “); 11. } 12. }; 13. 14. bindService(new Intent(”com.wissen.testApp.service.MY_SERVICE”), conn, Context.BIND_AUTO_CREATE); 15. … 复制代码 当应用程序绑定一个Service后,该应用程序和Service之间就能进行互相通信,通常,这种通信的完成依靠于我们定义的一些接口,请看下例: 1. package com.wissen.testApp; 2. 3. public interface IMyService { 4. public int getStatusCode(); 5. } 复制代码 这里定义了一个方法来获取Service的状态,但是Service是如何来使它起作用的呢?之前我们看到Service中有个返回IBinder对象的onBind方法,这个方法会在Service被绑定到其他程序上时被调用,而这个IBinder对象和之前看到的onServiceConnected方法中传入的那个IBinder是同一个东西。应用和Service间就依靠这个IBinder对象进行通信: 1. public class MyService extends Service { 2. private int statusCode; 3. private MyServiceBinder myServiceBinder = new MyServiceBinder(); 4. 5. @Override 6. public IBinder onBind(Intent intent) { 7. return myServiceBinder; 8. } 9. 10. public class MyServiceBinder extends Binder implements IMyService { 11. public int getStatusCode() { 12. return statusCode; 13. } 14. } 15. 16. … 17. } 复制代码 下列代码是说明getStatusCode是如何被调用的: 1. ServiceConnection conn = new ServiceConnection() { 2. @Override 3. public void onServiceConnected(ComponentName name, IBinder service) { 4. IMyService myService = (IMyService) service; 5. statusCode = myService.getStatusCode(); 6. Log.i(”INFO”, “Service bound “); 7. } 8. 9. … 10. }; 复制代码 或者,你也可以通过使用ServiceListener接口来达成相同的目的。 与远程Service通信(进程间Service通信): 如何两个进程间的Service需要进行通信,则需要把对象序列化后进行互相发送。 Android提供了一个 AIDL (Android接口定义语言)工具 来处理序列化和通信。这种情况下Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个 stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了,参见下例: 先创建一个IMyRemoteService.aidl文件,内容如下: 1. package com.wissen.testApp; 2. 3. interface IMyRemoteService { 4. int getStatusCode(); 5. } 复制代码 如果你正在使用eclipse的 Android插件,则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类,你要做的事就是去继承该Stub类: 1. package com.wissen.testApp; 2. 3. class RemoteService implements Service { 4. int statusCode; 5. 6. @Override 7. public IBinder onBind(Intent arg0) { 8. return myRemoteServiceStub; 9. } 10. 11. private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() { 12. public int getStatusCode() throws RemoteException { 13. return 0; 14. } 15. }; 16. 17. … 18. } 复制代码 当客户端应用连接到这个Service时,onServiceConnected方法将被调用,客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法: 1. … 2. ServiceConnection conn = new ServiceConnection() { 3. @Override 4. public void onServiceConnected(ComponentName name, IBinder service) { 5. IMyRemoteService myRemoteService = IMyRemoteService.Stub.asInterface(service); 6. try { 7. statusCode = myRemoteService.getStatusCode(); 8. } catch(RemoteException e) { 9. // handle exception 10. } 11. Log.i(”INFO”, “Service bound “); 12. } 13. 14. … 15. }; 复制代码 权限: 我们可以在AndroidManifest.xml文件中使用<service>标签来指定Service访问的权限: 1. <service class=”.service.MyServiceandroid:permission=”com.wissen.permission.MY_SERVICE_PERMISSION”> 2. <intent-filter> 3. <action android:value=”com.wissen.testApp.service.MY_SERVICE” /> 4. </intent-filter> 5. </service> 复制代码 之后应用程序要访问该Service的话就需要使用<user-permission>标签来指定相应的权限: 1. <uses-permission android:name=”com.wissen.permission.MY_SERVICE_PERMISSION”> 2. </uses-permission> 复制代码 http://www.fulema.com/viewthread.php?tid=5&extra=page%3D1
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值