Activity与Service数据交互:Binder,bindService(系列2)

service代码:

public class MyAppService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}


public class MyBinder extends Binder {

public MyBinder(){

}


public MyAppService getService() {
return MyAppService.this;
}
}
}


java代码 :

sc = new ServiceConnection() {


@Override
public void onServiceDisconnected(ComponentName name) {


}


@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBinder mBinder = (MyBinder) service;
myAppService = mBinder.getService();
}
};
Intent mIntent = new Intent(this, MyAppService.class);
bindService(mIntent, sc, Service.BIND_AUTO_CREATE);//执行bindService


Mainfest中添加

<service android:name=".MyAppService"/>

你可能感兴趣的:(Activity与Service数据交互:Binder,bindService(系列2))