`
nanjingjiangbiao_T
  • 浏览: 2600540 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Android开发--Service开启,关闭,绑定,解除

 
阅读更多

Android应用程序中有一项非常重要的技术--Service,它没有运行界面,运行在后台,但是有一点非常重要,Service并不是一个单独的进程,同时也不是一个线程,用来处理耗时的动作。

Service有两种使用方法,一种是start和stop,另一种是绑定服务。

当使用的是第一种方法时,直到用户停止这个服务,Service才会停止,即使是用户关闭这个Activity(应用程序),Service也一样会在后台运行。

当用户使用的是第二种方法时,一旦用户解除绑定或者停止了这个Activity(应用程序),Service也自动停止了,即它是随着Activity的生命周期运行的。

下面的截图是对Service的测试画面:

一下是程序的源代码:

public class A_Service01 extends Activity {
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private MyService myService;
	
	private ServiceConnection serviceConnection=new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			myService=null;
			Toast.makeText(A_Service01.this, "Service Failed", Toast.LENGTH_SHORT).show();
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			//myService=((MyService.MyBinder)service).getService();
			Toast.makeText(A_Service01.this, "Service Connected", Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_a__service01);
		button1=(Button)findViewById(R.id.button1);
		button2=(Button)findViewById(R.id.button2);
		button3=(Button)findViewById(R.id.button3);
		button4=(Button)findViewById(R.id.button4);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			final Intent intent=new Intent(A_Service01.this,MyService.class);
			startService(intent);
			}
		});
        button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			final Intent intent=new Intent(A_Service01.this,MyService.class);
			stopService(intent);
			}
		});
        button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final Intent intent=new Intent(A_Service01.this, MyService.class);
				bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
			}
		});
        button4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			unbindService(serviceConnection);	
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_a__service01, menu);
		return true;
	}

}

public class MyService extends Service {
	
	/*
	 * 出现这句话的原因是下面重载的onBind方法需要一个返回值
	 */
	private MyBinder myBinder=new MyBinder();

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.d("MyService", "Myservice onBind");
		return myBinder;
	}

	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		super.onRebind(intent);
		Log.d("MyService", "Myservice onRebind");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("MyService", "Myservice onUnbind");
		return super.onUnbind(intent);
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d("MyService", "Myservice onCreate");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.d("MyService", "flags---->"+flags);
		Log.d("MyService", "startId---->"+startId);
		Log.d("MyService", "Myservice onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("MyService", "Myservice onDestory");
	}


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

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics