Androidコンポーネント--サービス
6116 ワード
一.サービスとactivity通信
1.同じプロセスで
サービスでbinderをカスタマイズする、activityでサービスをバインドする際にこのbinderインスタンスを得る、このインスタンスによって両者間の通信を実現する.
public class MainActivity extends Activity {
private static final String TAG = "cc";
public MyBinder mBinder;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// Activity Service binder
mBinder = (MyBinder)iBinder;
//
mBinder.setOnTestListener(new MyBinder.OnTestListener() {
@Override
public void onTest(String str) {
Log.d(TAG, "receive msg from service: "+str);
}
});
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
bindService(intent,mConnection,BIND_AUTO_CREATE);
findViewById(R.id.test_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// mBinder , Service
mBinder.testMethod("hi, service.");
}
});
}
}
public class MyService extends Service {
private static final String TAG = "cc";
// MyBinder
private MyBinder mBinder = new MyBinder(this);
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;// mBinder
}
public void serviceMethod(String str){
Log.d(TAG, "receive msg from activity: " + str);
}
}
public class MyBinder extends Binder {
private static final String TAG = "cc";
private MyService mService;
private OnTestListener mListener;
public MyBinder(MyService service) {
this.mService = service;
}
public void testMethod(String str) {
// Activity Binder Service Service
mService.serviceMethod(str);
// mListener.onTest Activity
mListener.onTest("hi, activity.");
}
// MyBinder
public void setOnTestListener(OnTestListener listener) {
this.mListener = listener;
}
//
public interface OnTestListener {
void onTest(String str);
}
}
2.プロセス間
AIDLを使用するプロセス間通信は、全体のプロセスは単一プロセスと同様に、1つのBinderによって通信されているが、単一プロセスのBinderは自分がBinderクラスを継承することによって手動で実現され、プロセス間のBinderはAIDLによって自動的に生成する.簡単に言えば、AIDLはコードを生成するためのツールであり、最終的な目的はIMyAidlInterfaceを得ることである.JAvaというクラス
interface IMyAidlInterface {
void testMethod(String str);
}
public class AidlBinder extends IMyAidlInterface.Stub {
private MyService mService;
public AidlBinder(MyService service) {
this.mService = service;
}
@Override
public void testMethod(String str) throws RemoteException {
mService.serviceMethod(str);
}
}
ublic class MyService extends Service {
private static final String TAG = "zjy";
private AidlBinder mBinder = new AidlBinder(this);
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public void serviceMethod(String str) {
Log.d(TAG, "receive msg from activity: " + str);
}
}
public class MainActivity extends Activity {
private static final String TAG = "zjy";
public IMyAidlInterface mService;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mService = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
bindService(intent, mConnection, BIND_AUTO_CREATE);
findViewById(R.id.test_bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mService.testMethod("hi, service.");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
interface IMyCallbackListener {
void onRespond(String str);
}
import com.zjy.servicedemo.IMyCallbackListener;
interface IMyAidlInterface {
void testMethod(String msg);
void registerListener(IMyCallbackListener listener);
}
public class AidlBinder extends IMyAidlInterface.Stub {
private MyService mService;
private IMyCallbackListener mListener;
public AidlBinder(MyService service) {
this.mService = service;
}
@Override
public void testMethod(String str) throws RemoteException {
mService.serviceMethod(str);
mListener.onRespond("hi, activity");
}
@Override
public void registerListener(IMyCallbackListener listener) throws RemoteException {
mListener = listener;
}
}
try{
mService.registerListener(new IMyCallbackListener.Stub() {
@Override
public void onRespond(String str) throws RemoteException {
Log.d(TAG, "receive message from service: "+str);
}
});
} catch (RemoteException e){
e.printStackTrace();
}