远程Servie通信AIDL

不可以直接通过binder了。

1、先编写一个aidl文件,里边包含我们要通信的方法。(Android studio 有直接新建AIDL选项)

interface myInterface {
    /**
     * 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);


            //sayHello
            String sayHello(String name);

}

编译器会帮我们生成相应的java文件,不过现在在AS里面看不到。

接口中的抽象内部类Stub继承android.os.Binder类并实现myInterface接口,比较重要的方法是asInterface(IBinder)方法,该方法会将IBinder类型的对象转换成myInterface类型。

使用的时候我们只要定义好接口,再使用自动生成的Stub类即可。

2、实现Service

public class MyService extends Service{

  
  
  //实现Stub类,其实就是之前的自定义Binder myInterface.Stub stub
= new myInterface.Stub(){ @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }
    //重写adil文件的通信方法 @Override
public String sayHello(String name) throws RemoteException { //Toast.makeText(MyService.this,"hello"+","+name,Toast.LENGTH_SHORT).show();
       //Toast只能在UI线程
return "hello"+","+name; } }; @Nullable @Override public IBinder onBind(Intent intent) { Log.d("TAG","onBind");
    //返回自定义的binder
return stub; } @Override public void onCreate() { super.onCreate(); Log.d("TAG","onCreate"); } @Override public void onDestroy() { super.onDestroy(); Log.d("TAG","onDestory"); } }

manifest.xml 

        <service android:name=".MyService"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.MyService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

3、Activity

这跟之前不一样,不需要binder而是要一个接口的引用,绑定之后会将Service返回的binder转换为一个接口的实例。同样,需要自己实现ServiceConnection。

public class MainActivity extends AppCompatActivity {

    //这里用的是接口哦
    private myInterface myinterface;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            //将服务的stub(Binder)实例化我们的接口
            myinterface = myInterface.Stub.asInterface(iBinder);

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    //两个按钮
    Button hello,bind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bind = (Button)findViewById(R.id.bind);
        hello = (Button)findViewById(R.id.say);


        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

          //绑定 Intent intent
= new Intent(MainActivity.this,MyService.class); startService(intent); bindService(intent,conn,BIND_AUTO_CREATE); } }); hello.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try {

            //通信,在UI线程Toast String temp
= myinterface.sayHello("小明"); Toast.makeText(MainActivity.this,temp,Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } }); } }

没做特别说明的话,Service和Activity我猜测是运行在同一进程的同一线程中的,并且还是UI线程。因为可以在Service里面直接Toast。定义了远程Service后,他们就运行在不不同的进程中了,可以自己写一个sleep在service里面看看app有没有卡死来验证。还想用Toast的话,就要用消息队列了,发消息到UI线程里面来Toast

原文地址:https://www.cnblogs.com/wzben/p/5724718.html