码迷,mamicode.com
首页 > 其他好文 > 详细

IPC解决方案之 AIDL

时间:2016-04-19 00:07:54      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

android interface define language

跨进程通信前提:2个进程均已启动

技术分享

1,跨进程启动Service

 

Intent intent = new Intent();
//OTHER_SERVICE 另一进程Service包名+ClassName
intent.setComponent(
new ComponentName(OTHER_PACKAGE, OTHER_SERVICE)); startService(intent);

 

2,跨进程绑定Service

2.1 Service进程创建aidl

技术分享

2.2 onBind返回绑定对象

 @Override
    public IBinder onBind(Intent intent) {
        return new IMyAidlInterface.Stub() {
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }
        };
    }

2.3 在第1个进程调用bindService与unbindService

package com.example.superleeq.myapplication;

public class MainActivity extends Activity implements ServiceConnection {

    final static String OTHER_PACKAGE = "com.example.superleeq.myapplication2";
    final static String OTHER_SERVICE = "com.example.superleeq.myapplication2.MyService";
    boolean isbinded;

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

        initView();
    }

    private void initView() {
        findViewById(R.id.activity_main_button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(OTHER_PACKAGE, OTHER_SERVICE));
               
                isbinded = bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE);
            }
        });

        findViewById(R.id.activity_main_button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isbinded) {
                    unbindService(MainActivity.this);
                    isbinded = false;
                } else {
                    LogUtil.log("already unbindservice");
                }

            }
        });

     
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        LogUtil.log("onServiceConnected ComponentName name =  " + name + " , IBinder service = " + service);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        LogUtil.log("onServiceDisconnected ComponentName name = " + name);
    }
}

 

 

3,跨进程与Service通信

3.1 aidl folder + package + class必须两个工程完全一致

技术分享

3.2 app2 Service

package com.example.superleeq.myapplication2;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MyService extends Service {

    private String mData = "default data";
    private boolean running;

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("lq", "MyService.onCreate");

        new Thread(new Runnable() {
            @Override
            public void run() {
                running = true;

                while (running){
                    Log.e("lq", "MyService.mdata=" + mData);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("lq", "MyService.onDestroy");
        running = false;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("lq", "MyService.onBind Intent intent=" + intent);

        return new IMyAidlInterface.Stub() {
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }

            @Override
            public void setData(String data) throws RemoteException {
                mData = data;
            }
        };
    }


}

3.3 app1 mainActivity

package com.example.superleeq.myapplication;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.example.superleeq.myapplication.util.LogUtil;
import com.example.superleeq.myapplication2.IMyAidlInterface;


public class MainActivity extends Activity implements ServiceConnection {

    final static String OTHER_PACKAGE = "com.example.superleeq.myapplication2";
    final static String OTHER_SERVICE = "com.example.superleeq.myapplication2.MyService";
    boolean isbinded;
    IMyAidlInterface binder = null;

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

        initView();
    }

    private void initView() {
        findViewById(R.id.activity_main_button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //android5.0以后仅支持显示意图启动
                intent.setComponent(new ComponentName(OTHER_PACKAGE, OTHER_SERVICE));
                isbinded = bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE);
            }
        });

        findViewById(R.id.activity_main_button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                binder = null;

                if (isbinded) {
                    unbindService(MainActivity.this);
                    isbinded = false;
                } else {
                    LogUtil.log("already unbindservice");
                }

            }
        });

        final EditText editText = (EditText) findViewById(R.id.mInput);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (binder != null) {
                    try {
                        binder.setData(s.toString());
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        findViewById(R.id.setdata).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binder != null) {
                    try {
                        binder.setData(editText.getText().toString());
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        LogUtil.log("onServiceConnected ComponentName name =  " + name + " , IBinder service = " + service);
        //不能强制转换,否则报错
        binder = IMyAidlInterface.Stub.asInterface(service);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        //onServiceDisconnected()方法在正常情况下即使unbindService是不被调用的,它的调用时机是当Service服务被异外销毁时,例如内存的资源不足时
        LogUtil.log("onServiceDisconnected ComponentName name = " + name);
    }
}
    

 

IPC解决方案之 AIDL

标签:

原文地址:http://www.cnblogs.com/superleeq/p/5406199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!