android Service的用法

本文详细介绍了Android中Service的生命周期,包括通过StartService和bindService方式启动Service的不同生命周期回调方法。并给出了具体的代码示例。

Service是后台运行的服务,启动Service可以StartService或者bindservice

 

看看Service的生命周期。

 

当调用StartService时,先走oncreat方法,在走onstart方法。当stopservice时,走ondestory方法。

 

可以绑定一个Service,让它在后台执行,比方说后台播放歌曲等就是这样的。

 

当绑定一个Service时,用bindService方法,这时先走oncreate方法,在走onbind方法。当想停止Service时,用onunbindService方法,这时先走onUnbind方法,在走ondestory方法。

 

上代码:

package com.android.test;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public class TestService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		System.out.println("onBind");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		System.out.println("onCreate");
		super.onCreate();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		System.out.println("onStart");
		super.onStart(intent, startId);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("onDestroy");
		super.onDestroy();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onUnbind");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onRebind");
		super.onRebind(intent);
	}
}

 

上面这个是Service代码,下面这个是Activity的代码:
代码
package com.android.test;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1
= (Button) findViewById(R.id.button1);
Button button2
= (Button) findViewById(R.id.button2);
button1.setOnClickListener(
this);
button2.setOnClickListener(
this);
}

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(
"com.android.testservice");
switch(arg0.getId()) {
case R.id.button1:
// startService(intent);
bindService(intent, sc, BIND_AUTO_CREATE);
break;
case R.id.button2:
// stopService(intent);
unbindService(sc);
break;
}
}

ServiceConnection sc
= new ServiceConnection() {

public void onServiceConnected(ComponentName arg0, IBinder arg1) {
System.out.println(
"onServiceConnected");
}

public void onServiceDisconnected(ComponentName arg0) {
System.out.println(
"onServiceDisconnected");
}

};
}

 

哦,对了还要记得在manifest里面注册呢。

 

 


<service android:name=".TestService">
<intent-filter>
<action android:name="com.android.testservice"/>
</intent-filter>
</service>

当调用startService方法的时候,打印出来的log是:

  oncreate

  onstart

当调用stopService方法的时候,打印出来的log是:

  ondestory

当调用bindService和unbindServide方法时,打印出来的log是:

 

代码
11-25 12:30:47.837: INFO/System.out(335): onCreate
11-25 12:30:47.887: INFO/System.out(335): onBind
11-25 12:30:49.838: INFO/System.out(335): onUnbind
11-25 12:30:49.872: INFO/System.out(335): onDestroy

 

转载于:https://www.cnblogs.com/shang53880/archive/2010/11/25/1888087.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值