转:http://blog.csdn.net/shaojie519/article/details/6641066
Service是一种运行在后台的服务,一般很少与用户交互,所以没有可视化界面。
我们可以通过startService() 或者使用bindService()方法来绑定一个存在的service。
bindService是异步调用和Service进行绑定, 如果绑定成功, 则会调用ServiceConnection的onServiceConnected
当调用bindService方法后就会回调Activity的onServiceConnected,在这个方法中会向Activity中传递一个IBinder的实例,Acitity需要保存这个实例
在Service中需要创建一个实现IBinder的内部类(这个内部类不一定在Service中实现,但必须在Service中创建它)。
在OnBind()方法中需返回一个IBinder实例,不然onServiceConnected方法不会调用。
- 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;
- import android.widget.Toast;
- public class ServiceActivity extends Activity {
- /** Called when the activity is first created. */
- private Button start;
- private Button stop;
- private Button bind;
- private Button unbind;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- start =(Button) findViewById(R.id.start);
- stop = (Button) findViewById(R.id.stop);
- bind = (Button) findViewById(R.id.bind);
- unbind = (Button) findViewById(R.id.unbind);
- //启动一个service
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- System.out.println("start....");
- intent.setAction("com.shao.service.action.MyService");
- startService(intent);
- }
- });
- //结束一个service
- stop.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- System.out.println("stop....");
- intent.setAction("com.shao.service.action.MyService");
- stopService(intent);
- }
- });
- //绑定一个service
- bind.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- System.out.println("bind..");
- intent.setAction("com.shao.service.action.MyService");
- bindService(intent, conn, BIND_AUTO_CREATE);
- }
- });
- //解除绑定的service
- unbind.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Intent intent = new Intent();
- System.out.println("unbind..");
- intent.setAction("com.shao.service.action.MyService");
- unbindService(conn);
- }
- });
- }
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- // TODO Auto-generated method stub
- Toast.makeText(ServiceActivity.this,"disconn successful" ,Toast.LENGTH_LONG);
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
- Toast.makeText(ServiceActivity.this,"conn successful" ,Toast.LENGTH_LONG);
- }
- };
- }
MyService文件:
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.Parcel;
- import android.widget.Toast;
- public class MyService extends Service {
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- System.out.println("onBind.....");
- IBinder result = null;
- if ( null == result ) result = new MyBinder() ;
- Toast.makeText(this, "onBind",Toast.LENGTH_LONG);
- return result;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- System.out.println("onCreate.....");
- Toast.makeText(this, "onCreate",Toast.LENGTH_LONG);
- }
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- System.out.println("onDestory.....");
- Toast.makeText(this, "onDestroy",Toast.LENGTH_LONG);
- }
- @Override
- public void onStart(Intent intent, int startId) {
- // TODO Auto-generated method stub
- super.onStart(intent, startId);
- System.out.println("onStart...");
- Toast.makeText(this, "onStart",Toast.LENGTH_LONG);
- }
- public class MyBinder extends Binder {
- //此方法是为了可以在Acitity中获得服务的实例
- public MyService getService() {
- return MyService.this;
- }
- }
- }
Service需在mainifest文件中声明:
<service android:name="MyService">
<intent-filter>
<action android:name="com.shao.service.action.MyService"/>
</intent-filter>
</service>