文章目录
- 1、简介
- 2、功能实现
1、简介
通过bind 绑定本地服务,并且通过bind 访问服务里面的方法
通过点击 按钮,通过bind 调用服务里的方法
bind 绑定的服务生命周期 会随着调用者的结束而结束,也可以主动结束
2、功能实现
1)目录结构
2)、AndroidManifest.xml
添加服务
<service android:name=".MyService"/>
3)activity_main.xml 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="绑定服务" />
<Button
android:id="@+id/btn_bind_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="服务显示" />
<Button
android:id="@+id/btn_unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="解绑服务" />
</LinearLayout>
4) mainActivity.java
package myapplication21.lum.com.mytestview;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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 Activity {
private String TAG = "MainActivity: ";
private ServiceConnection conn;
private MyService.MyServiceBinder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn=new ServiceConnection() {
/**
* 当service 崩溃或系统被强制杀死的时候调用
* */
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG,"onServiceDisconnected");
}
/**
* 当服务访问者与服务绑定成功后调用
* */
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG,"onServiceConnected");
binder = (MyService.MyServiceBinder)service;
}
};
}
public void onClick(View view) {
Intent service=new Intent();
service.setClass(this,MyService.class);
switch (view.getId()) {
case R.id.btn_bind://绑定服务
bindService(service, conn,Context.BIND_AUTO_CREATE);
break;
case R.id.btn_bind_show://bind 调用服务的 函数
binder.show();
break;
case R.id.btn_unbind:// 解绑服务
unbindService(conn);
break;
}
}
}
- MyService.java 文件
package myapplication21.lum.com.mytestview;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private String TAG = "MyService: ";
/**
* 服务绑定后调用
* */
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return new MyServiceBinder();
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void 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) {
Log.d(TAG, "onRebind");
}
public void show() {
Log.d(TAG, "service....show");
}
/**
* 中介
* */
public class MyServiceBinder extends Binder {
public void show() {
MyService.this.show();
}
}
}