A应用
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="visizen.com.remoteservice" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<service android:name=".RemoteService" >
<intent-filter>
<action android:name="xxxxxxx" />
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
RemoteService
package visizen.com.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
/**
* Created by Administrator on 2015/12/18 0018.
*/
public class RemoteService extends Service{
@Override
public void onCreate() {
super.onCreate();
System.out.println("onCreate------------------");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("onDestroy------------------");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
MyBinder binder=new MyBinder();
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onUnbind------------------");
return super.onUnbind(intent);
}
public void sayHello(){
System.out.println("hello------------------");
}
private class MyBinder extends MyService.Stub{
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void hello() throws RemoteException {
sayHello();
}
}
}
MyService.aidl
// MyService.aidl
package visizen.com.remoteservice;
// Declare any non-default types here with import statements
interface MyService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
void hello();
}
B应用
MainActivity
package visizen.com.callremoteservice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import visizen.com.remoteservice.MyService;
public class MainActivity extends AppCompatActivity {
private MyServiceConnection myServiceConnection;
private MyService myService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void bindSrv(View view){
Intent intent=new Intent();
intent.setAction("xxxxxxx");
myServiceConnection=new MyServiceConnection();
bindService(intent,myServiceConnection,BIND_AUTO_CREATE);
}
public void callMethod(View view){
try {
myService.hello();
} catch (RemoteException e) {
e.printStackTrace();
}
}
private class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = MyService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
考虑A应用的aidl到B应用里,注意aidl文件,包名要一样