Tabla de contenido
1. MainActivity y main_cativity
Dos, MyService y configuración
1. MainActivity y main_cativity
Código de actividad principal
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bind_btn,unbind_btn,get_btn;
private MyService.MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
bind_btn = findViewById(R.id.bind_btn);
unbind_btn = findViewById(R.id.unbind_btn);
get_btn = findViewById(R.id.get_btn);
bind_btn.setOnClickListener(this);
unbind_btn.setOnClickListener(this);
get_btn.setOnClickListener(this);
}
//定义一个ServiceConnection 对象
private ServiceConnection connection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder){
Log.v("MainActivity","服务连接成功");
myBinder=(MyService.MyBinder)iBinder;
}
@Override
public void onServiceDisconnected(ComponentName componentName){
Log.v("MainActivity","服务断开连接");
}
};
@Override
public void onClick(View view){
switch (view.getId()) {
case R.id.bind_btn:
//绑定服务service
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, BIND_AUTO_CREATE);
Toast.makeText(this,"绑定成功",Toast.LENGTH_SHORT).show();
break;
case R.id.unbind_btn:
unbindService(connection);
Toast.makeText(this,"解绑成功",Toast.LENGTH_SHORT).show();
break;
case R.id.get_btn:
Toast.makeText(this, "获取服务"+myBinder.getCount(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
código actividad_principal
<?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:orientation="vertical"
tools:context=".activity.MainActivity">
<Button
android:id="@+id/bind_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定"/>
<Button
android:id="@+id/unbind_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解绑"/>
<Button
android:id="@+id/get_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"/>
</LinearLayout>
Dos, MyService y configuración
public class MyService extends Service {
private int count;
private boolean stop; //充当服务的状态//确定是否停止count计数
//定义onBinder方法返回的对象
private MyBinder binder =new MyBinder();
public class MyBinder extends Binder {
//获取 Service的运行状态
public int getCount(){
return count;
}
}
public IBinder onBind(Intent intent){
Log.v("MyService","绑定服务成功");
return binder;
}
@Nullable
@Override
public void onCreate() {
super.onCreate();
Log.v("MyService","创建成功");
new Thread(){
@Override
public void run() {
super.run();
while (!stop){
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
count++;
}
}
}.start();
}
@Override
public boolean onUnbind(Intent intent) {
Log.v("MyService","服务解除绑定");
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
this.stop = true;
Log.v("MyService","服务解除");
}
}
<aplicación> poner dentro </aplicación>
<service android:name=".service.MyService"/>
<!-- 上面的不行就试一试下面的配置 -->
<!--service android:name=".MyService"/-->
3. Demostración