解释还是在注释之中
MainActivity
package com.example.servicetest;
import androidx.appcompat.app.AppCompatActivity;
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.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button start_service;
private Button stop_service;
private Button start_bind;
private Button stop_bind;
private MyService.DownloadBinder downloadBinder;
//创建了ServiceConnection匿名类,onServiceConnected与onServiceDisconnected会在服务绑定与服务断开
//时调用
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//通过向下转型得到downloadBinder的实例
downloadBinder= (MyService.DownloadBinder) iBinder;
downloadBinder.Download();
downloadBinder.StopDownload();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start_service=findViewById(R.id.start_service);
stop_service=findViewById(R.id.stop_service);
start_bind=findViewById(R.id.start_Bind);
stop_bind=findViewById(R.id.stop_Bind);
start_service.setOnClickListener(this::onClick);
stop_service.setOnClickListener(this::onClick);
start_bind.setOnClickListener(this::onClick);
stop_bind.setOnClickListener(this::onClick);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_service:
Intent startService=new Intent(this,MyService.class);
startService(startService);//启动服务
break;
case R.id.stop_service:
Intent stopService=new Intent(this,MyService.class);
stopService(stopService);//停止服务只需在服务逻辑中任何位置调用stopself()就可以停止服务
break;
case R.id.start_Bind:
Intent startBind=new Intent(this,MyService.class);
//将MyService于MainActivity绑定,第一个参数是Intent对象,第二个是前面创建的
//ServiceConnection实例,第三个参数是标志位BIND_AUTO_CREATE,表示绑定后自动创建服务
//这回让Myservice中onCreate执行而onStartcommand不会执行
bindService(startBind,connection,BIND_AUTO_CREATE);
break;
case R.id.stop_Bind:
Intent stopBind=new Intent(this,MyService.class);
unbindService(connection);
break;
default:break;
}
}
}
Myservice
package com.example.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
//服务得在MainFest中注册
public class MyService extends Service {
public MyService() {
}
//服务创建时调用
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService","在运行onCreate");
}
//服务启动时调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService","在运行onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
//服务结束时调用
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyService","在运行onDestroy");
}
private DownloadBinder mBinder=new DownloadBinder();
class DownloadBinder extends Binder{
public void Download(){
Log.d("MyService","在运行Download()");
}
public void StopDownload(){
Log.d("MyService","在运行StopDownload()");
}
}
//这是service中唯一的一个抽象方法,得在子类中实现
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mBinder;
}
}
|