Android Aidl 愛之初體驗?

DomenicR65 7年前發布 | 9K 次閱讀 AIDL Android開發 移動開發

從前有個山 山里有個廟 廟里有個和尚 。。。。。一年過去了。老和尚跟小和尚說,孩子時候下去見母老虎了,山下的女人是老虎,師傅有一個要求您要學會aidl,當時小和尚一聽懵逼了,what is this?這時老和尚摸著小和尚的頭說:”學會aidl你就能下山了,遇到母老虎就可以跟我通信,師傅就馬上下山救您了“,

遇見了不要慌,就這樣小和尚很不開心,因為馬上要離開師傅了,不開心,寶寶不開心嘛,但是師傅掏出一本寶典 煥名”“aidl之遠程助攻母老虎72式”,小和尚拿著這本寶典開開心心的下山了,就這樣十年過去了,小和尚并沒有看到很多母老虎,他這時又想起師傅的話,默默的流淚,我是不是找了個假師傅,好尷尬呢?好了下面直接練習寶典!開大招!

Soeasy!

首先看第一招:入門式

第二招:惑騙式

先給大佬們上個圖

第三招 :進攻式

  1. 首先在src目錄下創建一個aidl目錄
  2. 創建一個IRemoteService.aidl文件
  3. 支持的數據類型 aidl支持的參數類型有int long boolean float double String

第四招:招虎式

這里創建一個方法進行通信

String baseAidl();

第五招:訓母式

1、這里需要創建一個RemoteService類

2、然后綁定服務類

3、創建一個Stub抽象類

4、在清單文件AndroidMainfest.xml中注冊 需要注意5.0以上系統需要設置包名 acton Name等

全部代碼如下

package com.example.arial.dgamedetail.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v4.os.IResultReceiver;
import android.util.Log;

import com.example.arial.dgamedetail.IRemoteService;

/**
 * Created by John on 2017/3/7.
 */

public class RemoteService extends Service {
    private static final String TAG = "RemoteService";


    public static abstract class Stub extends IRemoteService.Stub {

    }

    private IRemoteService.Stub mIRemoteService = new IRemoteService.Stub() {
        @Override
        public String baseAidl() throws RemoteException {
            return "您好,我是Kadan";
        }

        @Override
        public IBinder asBinder() {
            return super.asBinder();
        }

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

        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Intent intent1 = new Intent(this, RemoteService.class);
        bindService(intent1, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IResultReceiver.Stub.asInterface(service);
                Log.e(TAG, "onServiceConnected: ");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);

        return mIRemoteService.asBinder();
    }

    @Override
    public boolean bindService(Intent service, ServiceConnection conn, int flags) {
        return super.bindService(service, conn, flags);
    }
}

綁定服務

@Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Intent intent1 = new Intent(this, RemoteService.class);
        bindService(intent1, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IResultReceiver.Stub.asInterface(service);
                Log.e(TAG, "onServiceConnected: ");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);

        return mIRemoteService.asBinder();
    }
@Override
    public boolean bindService(Intent service, ServiceConnection conn, int flags) {
        return super.bindService(service, conn, flags);
    }

清單文件中注冊

<service android:name=".service.RemoteService" android:process=":remote">
            <intent-filter >
                <action android:name="com.example.arial.dgamedetail.service.RemoteService"/>
            </intent-filter>
        </service>

第六招 :放虎式

寫完之后別忘記主Activity中調用需要在onResume再次綁定服務

@Override
    protected void onResume() {
        super.onResume();
        Intent intent=new Intent();
        intent.setAction(Constant.ACTION);
        //5.0以上系統需要設置 
        intent.setPackage(Constant.PACKAGE);
        //綁定服務
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
              mIRemoteService=IRemoteService.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        },BIND_AUTO_CREATE);//自動創建
    }

另外給按鈕設置一個監聽事件我這里使用的是xml中添加onClick方法Aidl

/**
     * 設置發送消息的監聽事件
     * @param v
     */
    public void Aidl(View v)
    {
        //判斷當前服務是否為空
        try {
            if(mIRemoteService!=null){
                String text=mIRemoteService.baseAidl();
                if(text!=null)
                {
                    tv_aidl.setText(text);
                }
            }else{
                tv_aidl.setText("不好意思沒有獲取到消息");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

初始化TextView這里不再贅述看寶典吧

/**
     * 初始化UI組件
     */
    private void initView() {
        tv_aidl= (TextView) findViewById(R.id.tv_aidl);
    }

最后貼上AidlActivity完整代碼

package com.example.arial.dgamedetail;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.arial.dgamedetail.constant.Constant;

/**
 * Created by John on 2017/3/7.
 */

public class AidlActivity extends AppCompatActivity {
    //遠程服務類
    private IRemoteService mIRemoteService;
    //顯示的內容
    private TextView tv_aidl;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidl);
        initView();
    }

    /**
     * 初始化UI組件
     */
    private void initView() {
        tv_aidl= (TextView) findViewById(R.id.tv_aidl);
    }

    /**
     * 設置發送消息的監聽事件
     * @param v
     */
    public void Aidl(View v)
    {
        //判斷當前服務是否為空
        try {
            if(mIRemoteService!=null){
                String text=mIRemoteService.baseAidl();
                if(text!=null)
                {
                    tv_aidl.setText(text);
                }
            }else{
                tv_aidl.setText("不好意思沒有獲取到消息");
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent=new Intent();
        intent.setAction(Constant.ACTION);
        //5.0以上系統需要設置
        intent.setPackage(Constant.PACKAGE);
        //綁定服務
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
              mIRemoteService=IRemoteService.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        },BIND_AUTO_CREATE);//自動創建
    }
}

最后看下Constant封裝

package com.example.arial.dgamedetail.constant;

/**
 * Created by John on 2017/3/7.
 */

public class Constant {
    //包名
    public static final String PACKAGE="com.example.arial.dgamedetail";
    //清單文件注冊的action
    public static final String ACTION="com.example.arial.dgamedetail.service.RemoteService";
}

到這里基本技術了我只是個初級的就是aidl基本使用 然后進行簡單的遠程通信

可能以后還需要跟服務端進行通信等等,其實android與銀行對接用的就是這種機制,如果大家感興趣可以自己去網上了解學習哈!

 

來自:http://blog.csdn.net/qq_15950325/article/details/60756495

 

 本文由用戶 DomenicR65 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!