一.启动Service–startService
步骤,方法
1.
准备工作
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:onClick="doclick"
android:text="startService" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:onClick="doclick"
android:text="stopService" />
2.创建一个类继承Service实现生命周期中的四个方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
当点击的时候利用接口的方法启动服务
public void doclick(View v){
switch (v.getId()) {
case R.id.button1:
//注意,这就是普通的启动service的方法
//此处service1声明在头部为Intent对象
service1 = new Intent(MainActivity.this,MyService.class);
startService(service1);
break;
case R.id.button2:
stopService(service1);
break;
}
二.绑定Service–bindService
前两步都是一样的
继承service时实现的方法不一样
1.准备工作
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:onClick="doclick"
android:text="bind" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button3"
android:onClick="doclick"
android:text="unbind" />
2.创建一个类继承Service实现生命周期中的四个方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
//因为我们要试着把service对象闯入activity中用,而binder类中封装了这方面的方法,所以我们新建一个这样的类继承binder并返回一个binder对象
public class MyBinder extends Binder{
public MyBindService getService(){
return MyBindService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
//在这new一个binder对象并返回,那么只要service已绑定,就会有一个当前帮定的服务对象传出
return new MyBinder();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
3.创建ServiceConnection对象,第一个方法防止服务没解绑的意外推出,第二个方法连接服务之后执行的方法,我们之所有要用类还有一个原因就是在下面要bindService()和unbind()的时候有一个connection的对象参数
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
// TODO Auto-generated method stub
service = ((MyBinder)binder).getService();
}
};
4.添加事件
case R.id.button3:
service2 = new Intent(MainActivity.this,MyBindService.class);
bindService(service2, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(conn);
break;
还可以做一下兼容
当activity退出的时候自动销毁或解绑service
protected void onDestroy() {
// TODO Auto-generated method stub
unbindService(conn);
super.onDestroy();
}