Activity与Service数据交互:Binder、bindService的用法

时间:2023-03-10 04:51:55
Activity与Service数据交互:Binder、bindService的用法
 package com.lixu.jiaohu;

 import com.lixu.jiaohu.MyAppService.Mybind;

 import android.app.Activity;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener {
private ServiceConnection sc;
private MyAppService mMyAppService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3); btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
// 建立service连接
sc = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
Mybind mMybind = (Mybind) service;
mMyAppService = mMybind.getService();// 获取MyAppService的对象
}
};
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startService();
break;
case R.id.button2:
bindService();
break;
case R.id.button3:
String str = mMyAppService.getServiceValue();
Toast.makeText(this, str, 0).show();
break;
default:
break;
} } public void startService() {
mMyAppService.setServiceValue("你是:");// 设置MyAppService里面的公共变量的值
Intent intent = new Intent(this, MyAppService.class);
startService(intent);
} public void bindService() {
Intent it = new Intent(this, MyAppService.class);
bindService(it, sc, Service.BIND_AUTO_CREATE);// 建立service的bind连接
} @Override
protected void onDestroy() {
super.onDestroy();
Intent it = new Intent(this, MyAppService.class);
stopService(it);// 关闭服务
} }
 package com.lixu.jiaohu;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class MyAppService extends Service {
private String str; @Override
public void onCreate() {
Log.e("MyAppService", "onCreate开始了");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
str = str + "宝儿";
return super.onStartCommand(intent, flags, startId);
} // Activity bindService()方法开启后进入执行onBind方法
@Override
public IBinder onBind(Intent intent) { return new Mybind();
} public String getServiceValue() {
return str;
} public void setServiceValue(String str) {
this.str = str;
} public class Mybind extends Binder {
public MyAppService getService() {
return MyAppService.this; }
} @Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
} }