android AIDL 實踐之傳遞簡單字串

來源:互聯網
上載者:User

標籤:sage   generate   length   toast   one   bind   rtc   工程   imp   

*本demo的server和client寫反了

建立工程client,server在server端建立aidl檔案,內容:

// IMyAidlInterface.aidl
package com.example.server;

// Declare any non-default types here with import statements

interface IMyAidlInterface {

    String getString();
}

Make project自動在server\build\generated\source\aidl\debug\com\example\server產生java類

在client建立同樣檔案,同樣產生java類

*注意兩個的包名要一致

在server編寫service服務

package com.example.client;

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

import com.example.server.IMyAidlInterface;

/**
 * Created by luozhenlonghp on 2017/5/13.
 */

public class Myservice extends Service {
    public static final String TAG = "Myservice";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return result;
    }

    IMyAidlInterface.Stub result = new IMyAidlInterface.Stub(){

        @Override
        public String getString() throws RemoteException {
            return "you get it";
        }
    };

    @Override
    public void onCreate() {
        Log.i(TAG,"onCreate is called");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand is called");
        return super.onStartCommand(intent, flags, startId);
    }
}

 

在menifest檔案中註冊
<service android:name=".Myservice" 
 
//這裡表示進程名
android:process="com.test.myservice">

    <intent-filter>
        <action android:name="android.intent.action.RESPOND_VIA_MESSAGE"></action>
    </intent-filter>
</service>

 

在client端寫代碼綁定service
public void bindMyservice()
{
    Intent intent = new Intent();
    //這裡是action
intent.setAction("android.intent.action.RESPOND_VIA_MESSAGE");
//這裡是服務端的包名,本次demo寫反了
    intent.setPackage("com.example.client");
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
//這裡獲得執行個體
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);

            try {
                String result = iMyAidlInterface.getString();
                Toast.makeText(MainActivity.this,result , Toast.LENGTH_SHORT).show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    bindService(intent,connection,BIND_AUTO_CREATE);
}

大功告成

android AIDL 實踐之傳遞簡單字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.