Server端:
1、在AndroidManifest注册远程服务
<service
android:name=".NormalService"
android:enabled="true"
android:exported="true"
android:label="NormalService"
android:process=":remote" >
<intent-filter>
<action android:name="com.mars.NormalService" />
</intent-filter>
</service>
注意process要设置:remote才能运行在独立的进程
android:exported这个属性用于指示该服务是否能够被其他应用程序组件调用或跟它交互。如果设置为true,则能够被调用或交互,否则不能。设置为false时,只有同一个应用程序的组件或带有相同用户ID的应用程序才能启动或绑定该服务。
2、AIDL接口创建
</pre></p><p><span style="widows: auto;"></span><pre name="code" class="java">package com.mars.aidl;
interface IServiceAIDL {
int plus(int a, int b);
String toUpperCase(String str);
}
3、Service创建
package com.example.servicedemo;
import com.mars.aidl.IServiceAIDL;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class NormalService extends Service {
private String TAG = "NormalService";
private int count = 1;
MyBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind");
return mServiceBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int from = intent.getIntExtra("from", 0);
Log.d(TAG, "onStartCommand count " + count++ + " from " + from);
if (count == 10) {
stopSelf();
Log.d(TAG, "onStartCommand stopSelf");
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() executed");
Log.e(TAG, "process id is " + android.os.Process.myPid()+" pacageName "+getApplication().getPackageName());
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.d(TAG, "onRebind");
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind");
return super.onUnbind(intent);
}
class MyBinder extends Binder {
public void startWork() {
Log.d(TAG, "MyBinder startWork");
}
}
IServiceAIDL.Stub mServiceBinder = new IServiceAIDL.Stub(){
@Override
public int plus(int a, int b) throws RemoteException {
return a+b;
}
@Override
public String toUpperCase(String str) throws RemoteException {
if(str==null)
return null;
return str.toUpperCase();
}
};
}
Client端:
1、创建同包名的AIDL接口
2、调用远程Service
package com.mxw.client;
import com.mars.aidl.IServiceAIDL;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startNormalService(View v) {
}
public void stopNormalService(View v) {
}
public void goToB(View v) {
}
public void bindService(View v) {
Intent intent = new Intent();
intent.setAction("com.mars.NormalService");
intent.setPackage("com.example.servicedemo");
bindService(intent, connection, BIND_AUTO_CREATE);
}
public void unBindService(View v) {
unbindService(connection);
}
private IServiceAIDL myBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = IServiceAIDL.Stub.asInterface(service);
try {
int res = myBinder.plus(1, 2);
String str = myBinder.toUpperCase("mxw");
Log.e("onServiceConnected", "plus-res "+res+" toUpperCase-str "+str);
Log.e("onServiceConnected", "plus-res "+myBinder.plus(2, 2)+" toUpperCase-str "+myBinder.toUpperCase("mddTw"));
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
}