第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

android aidl 跨app調(diào)用那些事情

標(biāo)簽:
Android

今天大精和我聊天的时候,就问了aidl 跨app调用怎么写,我当然是一脸懵逼啊,不得不说大精想东西很深入,以下文章将给出答案

放一个成功的图吧:
TIM图片20171208142236.png

1.前提

抛开国产rom谈aidl 跨app调用的文章都是耍流氓,测试机魅族MX5E(16年买的千元机,我是穷逼),android版本5.0,为什么这么说呢,下文代码会有注释。

2.上代码

首先需要一个提供aidl service 的app,我们叫他A app
TIM图片20171208141102.png
(service在manifest配置如上图)
,aidl文件如下

// Declare any non-default types here with import statements
interface IMyAidlInterface {
    /**
     * 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);

            String getMessage();
}

MyService代码如下

public class MyService extends Service {

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

        }

        @Override
        public String getMessage() throws RemoteException {
            return "调用远端服务成功";
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("AppService", "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null != intent) {
            Log.e("AppService", "onStartCommand接收到的数据是:" + intent.getStringExtra("data"));
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return  stub;
    }
}

然后需要一个调用A app的aidl服务的B app
TIM图片20171208141705.png
这里注意B app工程需要和A app工程一样的aidl文件
然后如何调用A app的aidl服务呢?看下面的MainActivity代码

public class MainActivity extends AppCompatActivity {

    private IMyAidlInterface iMyAidlInterface;// 定义接口变量
    private ServiceConnection connection;

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

    private void bindRemoteService() {
        //注意:android 5.0之前是可以通过隐式意图打开其他app的服务的,5.0之后只能通过显式意图来打开.
        final Intent intentService = new Intent();
        //ComponentName的参数1:目标app的包名,参数2:目标app的Service完整类名
        intentService.setComponent(new ComponentName("com.example.zhouj.myapplication", "com.example.zhouj.myapplication.MyService"));
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                // 从连接中获取Stub对象
                iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
                // 调用Remote Service提供的方法
                try {
                    Toast.makeText(MainActivity.this, "获取到消息:" + iMyAidlInterface.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                // 断开连接
                iMyAidlInterface = null;
            }
        };
        //魅族手机上 需要先开启提供service的app 同时 service要启动的时候远程调用aidl才能收到消息
        startService(intentService);
        bindService(intentService, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connection != null)
            unbindService(connection);// 解除绑定
    }
}

这里要注意几个问题

  1. android 5.0之前是可以通过隐式意图打开其他app的服务的,5.0之后只能通过显式意图来打开.

2.魅族手机上 需要先开启提供service的app也就是说要先启动A app 同时 A app的service也要启动,才能让B app用A app aidl服务才能收到消息。

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
移動開發(fā)工程師
手記
粉絲
1萬
獲贊與收藏
137

關(guān)注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 1
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消