主介面程式碼:
public class BindServiceActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "BindServiceActivity";
private Button mBtBindService;
private Button mBtnUnbindService;
MyBindService myBindService;
boolean isBind=false;
ServiceConnection coon = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//繫結服務的時候偶觸發
Log.d(TAG, "onServiceConnected: ");
MyBindService.MyBinder myBinder = (MyBindService.MyBinder) service;
myBindService = myBinder.getService();
isBind=true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected: ");
//解綁的時候觸發
}
};
private Button mBtGetData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bind_service);
initView();
}
private void initView() {
mBtBindService = (Button) findViewById(R.id.bt_bind_service);
mBtnUnbindService = (Button) findViewById(R.id.btn_unbind_service);
mBtBindService.setOnClickListener(this);
mBtnUnbindService.setOnClickListener(this);
mBtGetData = (Button) findViewById(R.id.bt_get_data);
mBtGetData.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_bind_service:
Intent intent = new Intent(BindServiceActivity.this, MyBindService.class);
bindService(intent, coon, Context.BIND_AUTO_CREATE);
break;
case R.id.btn_unbind_service:
if (isBind){
unbindService(coon);
isBind=false;
}
break;
case R.id.bt_get_data:
Toast.makeText(myBindService, "亂數為:"+myBindService.getValues(), Toast.LENGTH_SHORT).show();
break;
}
}
}
服務程式碼:
public class MyBindService extends Service {
private static final String TAG = "MyBindService";
private IBinder iBinder;
private Random mRandom;
public MyBindService() {
}
public class MyBinder extends Binder{
MyBindService getService(){
return MyBindService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return iBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
iBinder = new MyBinder();
mRandom = new Random();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.d(TAG, "onRebind: ");
}
public int getValues(){
return mRandom.nextInt(100);
}
}
宣告:
service
android:name=".MyBindService"
android:enabled="true"
android:exported="true"></service>
xml程式碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BindServiceActivity">
<Button
android:id="@+id/bt_bind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="繫結服務"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_unbind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="解綁服務"
app:layout_constraintEnd_toEndOf="@+id/bt_bind_service"
app:layout_constraintStart_toStartOf="@+id/bt_bind_service"
app:layout_constraintTop_toBottomOf="@+id/bt_bind_service" />
<Button
android:id="@+id/bt_get_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="get data"
app:layout_constraintEnd_toEndOf="@+id/btn_unbind_service"
app:layout_constraintStart_toStartOf="@+id/btn_unbind_service"
app:layout_constraintTop_toBottomOf="@+id/btn_unbind_service" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MyBinder extends Binder{
MyBindService getService(){
return MyBindService.this;//獲取自己
}
}
初始化我們建立的類,並設定為成員變數
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
iBinder = new MyBinder();
mRandom = new Random();
}
private IBinder iBinder;
Intent intent = new Intent(BindServiceActivity.this, MyBindService.class);
bindService(intent, coon, Context.BIND_AUTO_CREATE);//繫結服務(引數:intent,繫結監聽,常數)
建立繫結監聽coon
/**
* 服務繫結連線監聽
*/
ServiceConnection coon = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {//繫結連線觸發
Log.d(TAG, "onServiceConnected: ");
MyBindService.MyBinder myBinder = (MyBindService.MyBinder) service;
myBindService = myBinder.getService();
isBind=true;
}
@Override
public void onServiceDisconnected(ComponentName name) {//繫結不連線觸發
Log.d(TAG, "onServiceDisconnected: ");
}
};
if (isBind){//沒繫結的話取消會報錯
unbindService(coon);//取消服務
isBind=false;
}
流程圖