日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android廣播和消息跨進程通信并返回數據

作者:`TooMee` 更新時間: 2022-07-12 編程語言

廣播結合消息跨進程通信

需求:

要實現一個在Framework中與上層app進行通信,不添加接口去實現。


項目描述:

項目中,我是在framework中進行獲取遙控器Mac信息,但是這些數據要去上層的app中顯示出來。但是不想去使用AILD的方式去實現進程之間通信。然后就想到了使用廣播結合消息去實現上述功能。


直接上代碼,重點代碼會使用start - end 標識出來; 此代碼只貼重點部分

// Android App層
public class JinJuManager implements Handler.Callback {
    private final Handler mHandler = new Handler(Looper.getMainLooper(), this);

    //start; framework中發送過來消息會回調到handleMessage
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
        case 0:
            if (msg.obj instanceof Bundle) {
                Bundle result = (Bundle) msg.obj;
                //獲取到framewrok層發送過來的Mac信息
                String macAddress = result.getString("MacAddress");
                mActivity.updateMacAddress(macAddress);
                Log.d(TAG, "Response -> MacAddress: " + macAddress);
            } else {
                Log.d(TAG, "Response is null.");
            }
            return true;
        default:
            return false;
        }

    }
   // end;   

    // start 首先創建一個Messenger對象出來,然后將要傳遞的數據通過bundle(key,vlue)形式進行保存。
    // 然后使用廣播把數據發送到framework端進行處理
    public void requestMacAddress(Context context, int vendorId, int productId) {
        Messenger messenger = new Messenger(mHandler);

        Intent intent = new Intent("com.toptech.action.REQUEST_MAC_ADDRESS");
        
        Bundle bundle = new Bundle();
        //相當于將messnger對象保存在binder中,然后跨進程通信
        bundle.putBinder("Messenger", messenger.getBinder());
        bundle.putInt("VendorId", vendorId);
        bundle.putInt("ProductId", productId);
        intent.putExtras(bundle);
        context.sendBroadcast(intent);
    }
    // end;
}

// framework層
public class UsbDeviceManager extends BroadcastReceiver {

    public static final int VID = 4310;
    public static final int PID = 45062;
    private final Context mContext;

    public UsbDeviceManager(Context context) {
        mContext = context;

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.toptech.action.REQUEST_MAC_ADDRESS");

        mContext.registerReceiver(this, filter);
    }
    //start; 接收到攜帶數據的廣播
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        Log.d(TAG, "action: " + action);

        if ("com.toptech.action.REQUEST_MAC_ADDRESS".equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras == null || extras.isEmpty()) {
                return;
            }
            //拿到app層發送過來的VID,PID
            int vendorId = extras.getInt("VendorId", 0);
            int productId = extras.getInt("ProductId", 0);
            if (vendorId == 0 && productId == 0) {
                return;
            }

            Log.d(TAG, "Request mac address -> vid: " + vendorId + ", pid: " + productId);
            //這個方法是在framework中獲取Mac信息的方法
            String macAddress = getMacAddress(vendorId, productId);
            if (TextUtils.isEmpty(macAddress)) {
                return;
            }
            //這里就是相當于獲取到app層發送過來的messenger對象
            IBinder binder = extras.getBinder("Messenger");
            if (binder == null) {
                return;
            }

            //將獲取到的mac信息存入MacAddress中
            Bundle result = new Bundle();
            result.putString("MacAddress", macAddress);
            
            Message message = Message.obtain();
            message.what = 0;
            message.obj = result;
            //向上層發送消息
            Messenger messenger = new Messenger(binder);
            try {
                messenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            return;
        }
    }
    // end;

總結:

app層(創建廣播,創建消息,發送數據) -> framewrok層(接收廣播,接收消息,發送數據) -> app層(回調handleMessage,接收數據)

原文鏈接:https://blog.csdn.net/qq_42989195/article/details/125716222

欄目分類
最近更新