利用ContentProvider call方法实现跨进程调用

与其他三种跨进程(BroadCast, Intent, AIDL)相比优缺点参考文章:
https://blog.csdn.net/cnzx219/article/details/46645519
使用方法:
调用者:

"xxx.PROVIDER_CALL" />
try {
    Bundle bundle = new Bundle();
    RemoteCallback listener = new RemoteCallback(new RemoteCallback.OnResultListener() {
        @Override
        public void onResult(Bundle result) {
            Log.d(TAG, "receive result do something");
        }
    }, new Handler());
    bundle.putParcelable("listener", listener);
    Uri uri = Uri.parse("you wanted content provider uri");
    getContentResolver().call(uri, "LISTEN", null, bundle);
    Log.d(TAG, "call provider");
} catch (Exception e) {
    e.printStackTrace();
}

接收者:

实现ContentProvider
在call方法中添加:
if (TextUtils.equals("LISTEN", method)) {
    final RemoteCallback callback = (RemoteCallback) extras.getParcelable("listener");
    if (callback != null) {
        mListener = new Runnable() {
            @Override
            public void run() {
                callback.sendResult(new Bundle("you want send info"));
            }
        };
    }
}

你可能感兴趣的:(android)