1.创建一个service重写三个方法onCreate()、onStartCommand、和onDestroy()。
@Override
public void onCreate() {//服务创建的时候调用。
super.onCreate();
Log.d("MyService", "onCreate excuted ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//服务每次启动的时候调用。
Log.d("MyService", "onStartCommand excuted ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {//服务销毁的时候调用。
super.onDestroy();
Log.d("MyService", "onDestroy executed ");
}
@Override
public IBinder onBind(Intent intent) {
//抽象方法,必须在子类中实现,先忽略。
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
注意:如果想让服务自己停下来,只需要在MyService的任何一处调用stopSelf()方法就能让服务停止。
2.活动和服务进行通信
onBind()方法,使活动可以指挥服务去干什么,服务就去干什么。
例如:我们希望在MyService里提供一个下载功能,然后活动可以决定合时下载,以及随时查看下载进度。实现这个功能首先创建一个专门的Binder对象对下载功能进行管理。
private DownloadBinder mBinder=new DownloadBinder();
class DownloadBinder extends Binder {
public void startDownload(){
Log.d(
"MyService", "startDownload executed ");
}
public int getProgress(){
Log.d("MyService", "getProgress executed ");
return 0;
}
}
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate excuted ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand excuted ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyService", "onDestroy executed ");
}
activity_main.xml
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind Service"/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Unbind Service"/>
MainActivity
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection=new ServiceConnection() {//创建一个人匿名类
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//活动与服务成功绑定的时候调用
downloadBinder=(MyService.DownloadBinder) service;
//向下转型获得DownloadBinder实例
downloadBinder.startDownload();
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
//活动与服务连接断开的时候调用。
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button bindService=(Button) findViewById(R.id.bind_service);
Button unbindService=(Button) findViewById(R.id.unbind_service);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//利用点击事件完成绑定
switch (v.getId()){
case R.id.bind_service:
Intent bindIntent=new Intent(this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
//绑定服务,三个参数,第一个参数是Intent对象,
第二个参数是前面创建的ServiceConnection实例,第三个参数是一个标志位,
这里传入BIND_AUTO_CREATE表示活动与服务进行绑定后自动创建服务。
break;
case R.id.unbind_service://解绑服务
unbindService(connection);
break;
default:
break;
}
}
}
这里面onCreate()方法会执行,onStartCommand()方法不会执行。
注意:任何一个服务在整个应用程序范围内都是通用的不仅可以和一个也可以和其他的活动进行绑定,而且绑定完场后他们可以获得相同的实例。
网友评论