创建安卓应用

编写布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@color/colorAccent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/edtText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<Button
android:id="@+id/btnBindSerbice"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/bind_service"
android:onClick="doBindService"/>
<Button
android:id="@+id/btnunBindSerbice"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/unbind_service"
android:onClick="dounBindService"/>
</LinearLayout>
创建customservice 自定义服务类

package net.zjs.bind_unbind_service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
/**
* 功能:自定义服务类
*/
public class CustomService extends Service {
private final String TAG="bind_unbind_service";//标记
//创建回调方法
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"CustomService.onCreate() invoked.");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
//销毁回调方法
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"CustomService.onDestroy() invoked.");
}
//绑定回调方法
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"CustomService.onBind() invoked");
//获取从窗口传递过来的数据
String message=intent.getStringExtra("message");
//显示数据
Log.d(TAG,"恭喜,成功绑定服务!主窗口传递的数据:"+message);
//返回绑定器对象
return new Binder();
}
}
编写主界面类

package net.zjs.bind_unbind_service;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private final String TAG="bind_unbind_service";//标记
private ServiceConnection conn;//服务连接对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*绑定服务*/
public void doBindService(View view){
//创建意图,显式指明要绑定的服务
Intent intent=new Intent(this,CustomService.class);
//让意图携带数据
intent.putExtra("message","安卓开发还真是有趣");
//创建服务谅解对象
conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG,"服务已连接");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG,"服务断开连接");
}
};
//按意图绑定服务
bindService(intent,conn, Service.BIND_AUTO_CREATE);
}
/*解绑服务*/
public void dounBindService(View view){
//判断服务连接是否为空
if(conn!=null){
//解绑服务
unbindService(conn);
}
}
/*销毁窗口时解绑服务*/
@Override
protected void onDestroy() {
super.onDestroy();
//判断服务连接是否为空
if(conn!=null){
//解绑服务
unbindService(conn);
}
}
}
运行效果

- 提示:在Logcat中才能看到运行成功与否
