安卓service需要注意的东西

由于是新手,在搞安卓service这一块会犯一些迷糊,记下来以防以后再出错时能及时找到信息。

1.默认情况下,即不对service做任何处理的情况下,service默认是和启动它的activity运行在同一进程同一线程下的。如下

03-20 16:55:18.574: I/System.out(24044): the activity process id is 24044
03-20 16:55:18.579: I/System.out(24044): the activity thread id is 1
03-20 16:55:23.149: I/System.out(24044): service oncreate
03-20 16:55:23.149: I/System.out(24044): service onstart
03-20 16:55:23.149: I/System.out(24044): the service process id is 24044
03-20 16:55:23.149: I/System.out(24044): the service thread id is 1
所以,此时不能在service做任何耗时的操作,需要在service里新开线程来完成这些耗时操作


2.onStart()和onBind()两个方法的异同

 通过startService
    Service会经历 onCreate --> onStart
    stopService的时候直接onDestroy

   如果是 调用者 直接退出而没有调用stopService的话,Service会一直在后台运行。
   下次调用者再起来仍然可以stopService。

  通过bindService   
    Service只会运行onCreate, 这个时候 调用者和Service绑定在一起

   调用者退出了,Srevice就会调用onUnbind-->onDestroyed
   所谓绑定在一起就共存亡了。

程序输出如下

1 通过startService

假设我第一次调用startService方法:service会执行下面两个方法

03-20 17:00:36.234: I/System.out(24044): oncreate
03-20 17:00:36.234: I/System.out(24044): onstart
然后此时直接退出应用,service不会执行onDestroy()方法,也就是说此时的service会在后台运行。如果我重新打开应用,再次执行startService方法,程序输出如下:

03-20 17:02:40.039: I/System.out(24044): onstart
此时应用边不会再执行oncreate方法。

如果我在程序退出之前执行stopService方法,那么此时service会执行onDestroy方法:

03-20 17:04:41.079: I/System.out(24044): onDestroy
这时我们再次退出应用的话,由于service已经被销毁,那么此时service便不会在后台运行


2. 通过bindService   

如果我们不调用startService方法而是直接调用bindService方法,此时程序输出如下:

03-20 17:07:26.104: I/System.out(24044): oncreate
03-20 17:07:26.104: I/System.out(24044): onbind

service没有执行onstart方法,如果此时我们执行unbindService方法或者直接退出应用,程序都会执行onDestroy方法:

03-20 17:08:06.259: E/MyService(24044): start onUnbind~~~//执行unbindService会执行service的<span style="font-family: Arial, Helvetica, sans-serif;">onUnbind方法,直接退出应用不会执行这个方法</span>
03-20 17:08:06.259: I/System.out(24044): onDestroy
此时直接销毁这个service,也就是说,这个service不会在后台运行

综上,我们要想让一个service在后台运行,必须调用startservice这个方法,让一个service不在后台运行,要调用它的onDestroy方法

我将测试程序贴出来,供大家测试:

public class MainActivity extends Activity implements OnClickListener {

	private MyService mMyService;
	private TextView mTextView;
	private Button startServiceButton;
	private Button stopServiceButton;
	private Button bindServiceButton;
	private Button unbindServiceButton;
	private Context mContext;

	// 这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		// 当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			mMyService = ((MyService.MyBinder) service).getService();
			mTextView.setText("I am frome Service :"
					+ mMyService.getSystemTime());
		}

		public void onServiceDisconnected(ComponentName name) {

		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		System.out.println("the activity process id is " + android.os.Process.myPid());
		System.out.println("the activity thread id is " + Thread.currentThread().getId());
		setupViews();
	}

	public void setupViews() {

		mContext = MainActivity.this;
		mTextView = (TextView) findViewById(R.id.text);

		startServiceButton = (Button) findViewById(R.id.startservice);
		stopServiceButton = (Button) findViewById(R.id.stopservice);
		bindServiceButton = (Button) findViewById(R.id.bindservice);
		unbindServiceButton = (Button) findViewById(R.id.unbindservice);

		startServiceButton.setOnClickListener(this);
		stopServiceButton.setOnClickListener(this);
		bindServiceButton.setOnClickListener(this);
		unbindServiceButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == startServiceButton) {
			Intent i = new Intent();
			i.setClass(MainActivity.this, MyService.class);
			mContext.startService(i);
		} else if (v == stopServiceButton) {
			Intent i = new Intent();
			i.setClass(MainActivity.this, MyService.class);
			mContext.stopService(i);
		} else if (v == bindServiceButton) {
			Intent i = new Intent();
			i.setClass(MainActivity.this, MyService.class);
			mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
		} else {
			mContext.unbindService(mServiceConnection);
		}
	}

	@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;
	}

}

service代码:

public class MyService extends Service {


	// 定义个一个Tag标签
	private static final String TAG = "MyService";
	// 这里定义吧一个Binder类,用在onBind()有方法里,这样Activity那边可以获取到
	private MyBinder mBinder = new MyBinder();

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onbind");
		return mBinder;
	}
	@Override
	public void onCreate() {
		System.out.println("oncreate");
		super.onCreate();
	}
	@Override
	public void onStart(Intent intent, int startId) {
		System.out.println("onstart");
		super.onStart(intent, startId);
		System.out.println("the service process id is " + android.os.Process.myPid());
		System.out.println("the service thread id is " + Thread.currentThread().getId());
	}
	@Override
	public void onDestroy() {
		System.out.println("onDestroy");
		super.onDestroy();
	}
	@Override
	public boolean onUnbind(Intent intent) {
		Log.e(TAG, "start onUnbind~~~");
		return super.onUnbind(intent);
	}

	public class MyBinder extends Binder {
		MyService getService() {
			return MyService.this;
		}
	}

}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值