在 Android
Service
未被绑定的情况下,调用 unbindService(connection)
会报错误:service Service not registered
, 所以在解除绑定前要先判断连接是否处于绑定状态.
MainActivity
类:
public class MainActivity extends AppCompatActivity {
private MyService.DownloadBinder downloadBinder;
private boolean isBind = false; //是否绑定服务
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button startService = (Button)findViewById(R.id.start_service);
final Button stopService = (Button)findViewById(R.id.stop_service);
final Button bindService = (Button)findViewById(R.id.bind_service);
final Button unbindService = (Button)findViewById(R.id.unbind_service);
startService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, MyService.class);
startService(startIntent);
}
});
stopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent stopIntent = new Intent(MainActivity.this, MyService.class);
stopService(stopIntent);
}
});
bindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent bindIntent = new Intent(MainActivity.this, MyService.class);
isBind = bindService(bindIntent, connection, BIND_AUTO_CREATE); //绑定服务
}
});
unbindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBind){ //解除绑定前要判断服务是否是绑定状态,其实此处应该还要加入 connection 是否为null 的判断
unbindService(connection); //解绑服务
isBind = false;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder)service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
MyService
类:
public class MyService extends Service {
private DownloadBinder mBinder = new DownloadBinder();
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate execute");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand execute");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyService", "onDestroy execute");
}
class DownloadBinder extends Binder{
public void startDownload(){
Log.d("MyService", "startDownload executed");
}
public int getProgress(){
Log.d("MyService", "getProgress executed");
return 0;
}
}
}