android 远程Service以及AIDL的跨进程通信
package com.example.servicetest.MyAIDLService;
interface MyAIDLService {
int plus(int a, int b);
String toUpperCase(String str);
}
public class MainActivity extends Activity implements OnClickListener {
Button btn_start,btn_stop,btn_bind,btn_unbind;
MyAIDLService mAIDLService;
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAIDLService=MyAIDLService.Stub.asInterface(service);
try {
int result=mAIDLService.plus(8, 7);
String upperStr = mAIDLService.toUpperCase("hello world");
Log.d("info", "--result---" + result);
Log.d("info", "--upperStr---" + upperStr);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start=(Button) findViewById(R.id.btn_start);
btn_stop=(Button) findViewById(R.id.btn_stop);
btn_bind=(Button) findViewById(R.id.btn_bind);
btn_unbind=(Button) findViewById(R.id.btn_unBind);
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_bind.setOnClickListener(this);
btn_unbind.setOnClickListener(this);
Log.e("info", "process id is " + Process.myPid());
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_start){
Intent intent=new Intent(this,MyService.class);
startService(intent);
}
else if(v.getId()==R.id.btn_stop){
Intent intent=new Intent(this, MyService.class);
stopService(intent);
}
else if(v.getId()==R.id.btn_bind){
Intent intent=new Intent(this, MyService.class);
bindService(intent, connection,BIND_AUTO_CREATE);
}
else if(v.getId()==R.id.btn_unBind){
unbindService(connection);
}
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Bind Service"
/>
</RelativeLayout>
public class MainActivity extends Activity {
private MyAIDLService mAIDLService;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAIDLService=MyAIDLService.Stub.asInterface(service);
try {
int result=mAIDLService.plus(50, 48);
String upperStr=mAIDLService.toUpperCase("hello world");
Log.e("info","---result---"+result);
Log.e("info","upperStr--"+upperStr);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.myAIDL");
bindService(intent, connection, BIND_AUTO_CREATE);
}
});
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。