AIDL例子

1、aidl接口文件(包名)服務端和客戶端必須保持一致。

2、客戶端調用服務端的接口時,有二種方法。這裏需要說明的是。setaction中內容是在服務端manifest中service標籤的<action>字段。 setpackage字段是服務端的包名即manifest.xml中填寫的包名。

// 第一種方式 第一個參數是服務端package名, 第二個參數是服務端service類路徑
       intent.setComponent(new ComponentName("com.example.vvv.test", "com.example.vvv.service.MyService"));
        // 第二種方式 服務端的manifest必須聲明action字段
//        intent.setAction("com.example.aidl");
//        intent.setPackage("com.example.vvv.test");
        this.getApplicationContext().bindService(intent, sc, Service.BIND_AUTO_CREATE);

其中第一中方法服務端的manifest文件的service不一定要聲明<action>字段

第二種方法服務端的manifest文件的service必須要聲明<action>字段

服務端代碼:

    manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vvv.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.example.vvv.service.MyService"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.example.aidl"/>
            </intent-filter>
            </service>


    </application>

</manifest>

服務端服務類:

   

package com.example.vvv.service;

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

import com.example.aidl.IMyAidlInterface;


/**
 * Created by VVV on 2018/5/6.
 */

public class MyService extends Service {

    private final IMyAidlInterface.Stub mBinder  = new IMyAidlInterface.Stub() {
        @Override
        public int add(int num1, int num2) throws RemoteException {
            return num1 + num2;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("service", "服務端被啓動");
        return  mBinder ;
    }

}

客戶端:

    

package com.example.client;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.aidl.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    private EditText et1;
    private EditText et2;
    private EditText et3;
    private Button btn;

    private IMyAidlInterface iMyAidlInterface;

    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidlInterface =  IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iMyAidlInterface = null;
        }
    };

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

    }


    private void initEvent() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                int num1 = Integer.valueOf(et1.getText().toString().trim());
                int num2 = Integer.valueOf(et2.getText().toString().trim());
                int result = 0;
                try {
                    result = iMyAidlInterface.add(num1, num2);

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                et3.setText(String.valueOf(result));

            }
        });
    }

    private void initViews() {
        et1 = (EditText) findViewById(R.id.num1);
        et2 = (EditText) findViewById(R.id.num2);
        et3 = (EditText) findViewById(R.id.result);

        btn = (Button) findViewById(R.id.btn1);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent();
;

        // 第一種方式 第一個參數是服務端package名, 第二個參數是服務端service類路徑
       intent.setComponent(new ComponentName("com.example.vvv.test", "com.example.vvv.service.MyService"));
        // 第二種方式 服務端的manifest必須聲明action字段
//        intent.setAction("com.example.aidl");
//        intent.setPackage("com.example.vvv.test");
        this.getApplicationContext().bindService(intent, sc, Service.BIND_AUTO_CREATE);

    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(sc);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章