AIDL实现进程间通讯(Android 同步调用)

2018-01-03  本文已影响0人  小小葡萄枝

服务端代码:

package com.android.aidl;

interface IAIDLConfigService {
    int setHomePage(String homepage);
}
LOCAL_SRC_FILES += \ $(call all-Iaidl-files-under, src)

LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/src/
package com.android.aidl;

import com.android.aidl.IAIDLConfigService;

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

import com.android.browser.BrowserSettings;
import com.android.browser.UrlUtils;

public class AIDLConfigService extends Service {

    static final String TAG = "AIDLConfigService";

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "====>onBind().");
        IBinder binder = new ImplAIDLConfigService();
        Log.d(TAG, "====>onBind().binder = " + binder);
        return binder;
    }

    public class ImplAIDLConfigService extends IAIDLConfigService.Stub{
        @Override
        public int setHomePage(String homepage) throws RemoteException {
            Log.d(TAG, "setHomePage(), homepage = " + homepage);
            if (homepage != null) {
                BrowserSettings.getInstance().setHomePage(UrlUtils.smartUrlFilter(homepage,false));
            }
            return 123;
        }
    }

}
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.browser">
......
        <service android:name="com.android.aidl.AIDLConfigService">
            <intent-filter>
                <action android:name="com.andorid.aidl.BrowserDataConfig" />
            </intent-filter>
        </service>

    </application>

</manifest>

客户端代码:

package com.android.aidl;

interface IAIDLConfigService {
    int setHomePage(String homepage);
}

Note:
客户端IAIDLConfigService.aidl跟服务端文件需要完全一样, 包括目录结构也需要一样,都在src\com\android\aidl\目录下

LOCAL_SRC_FILES += \ $(call all-Iaidl-files-under, src)

LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/src/
import android.content.Context;

import android.app.Service;
import android.content.Intent;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import com.android.aidl.IAIDLConfigService;

import android.util.Log;

public class AIDLBrowserImpl {
    public final String TAG = "AIDLBrowserImpl";
    public final static String AIDL_BROWSER_ACTION = "com.andorid.aidl.BrowserDataConfig";

    private boolean mIsBound;
    private IAIDLConfigService mBoundService = null;

    private void bindService(Context context) {
        Log.e(TAG, "bindService(): enter");
        Intent i = new Intent();
        i.setAction(AIDL_BROWSER_ACTION);
        i.setPackage("com.android.browser");
        context.bindService(i, mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    private void unbindService(Context context) {
        if (mIsBound) {
            context.unbindService(mConnection);
            mIsBound = false;
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            Log.d(TAG, "-->> onServiceConnected() enter.");
            mBoundService = IAIDLConfigService.Stub.asInterface(service);
            Log.d(TAG, "-->> onServiceConnected(),  boundService = " + mBoundService);
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
            Log.d(TAG, "-->> onServiceDisconnected()");
        }
    };

    public void init(Context context) {
        bindService(context);
    }

    public void free(Context context) {
        unbindService(context);
    }

    public boolean setHomePage(Context context, String link, String name){

        Log.d(TAG, "setHomePage(): enter, link = " + link + ", name = " + name);
        int result = -1;

        try {
                if(mBoundService == null) return false;
                result = mBoundService.setHomePage(link);
                Log.e(TAG,"mBoundService.setHomePage() result = " + result);
        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        } 

        if(result != 0) return false;

        return true;
    }

}
package com.android.aidltest;

import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.Context;
import android.widget.Toast;
import android.app.Activity;
import android.util.Log;

import com.sprd.aidl.adapter.porting.AIDLBrowserImpl;

public class MainActivity extends Activity {

    public static final String TAG = "AIDLTest";
    AIDLBrowserImpl mAIDLBrowserImpl;

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

        final Button btnTestBrowser = (Button) findViewById(R.id.btnTestBrowser);
        btnTestBrowser.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                testBrowserDataConfig(true);
            }
        });

        mAIDLBrowserImpl = new AIDLBrowserImpl();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: this=" + this);
        if(mAIDLBrowserImpl != null) {
            mAIDLBrowserImpl.init(this);
        }
    }

    @Override
    protected void onStop() {
        Log.i(TAG, "onStop");
        mAIDLBrowserImpl.free(this);
        super.onStop();
    }

    private void testBrowserDataConfig(boolean isNormalTest) {

        Log.d(TAG, "Test aidl browser data config beging ... isNormalTest = " + isNormalTest);

        //Test homepage
        {
            String link = "cn.bing.com";
            String name = "BingBing";

            mAIDLBrowserImpl.setHomePage(this, link, name);
        }

        Toast.makeText(this, "Test aidl browser data config end", Toast.LENGTH_SHORT).show();

        return;
    }

}

小结

上一篇下一篇

猜你喜欢

热点阅读