Android中AIDL跨进程访问机制

AILD机制:
通俗的讲就是访问别人的服务。
使用情况:
访问别人的第三方服务框架
例如:
自己的Activity访问别人的service
这里写图片描述

案例:跨应用进程绑定访问service

服务端

1、编写服务端
android_day11_service
PlayMusicService.java

package com.example.android_day11_service;

import com.example.android_day11_service.MusicInterface.Stub;

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

public class PlayMusicService extends Service{

    /**
     * 当调用bindService时执行  
     */
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
    //等第二步编写完接口之后
    class MyBinder extends Stub{
        public void play() throws RemoteException {
            Log.i("info", "play...");
        }
        public void next() throws RemoteException {
            Log.i("info", "next...");
        }
        public void pre() throws RemoteException {
            Log.i("info", "pre...");
        }
    }

}

2、编写接口也就是AIDL(MusicInterface.aidl)
MusicInterface.aidl
后缀名改了之后根文件自动生成文件
这里写图片描述

package com.example.android_day11_service;

/**
 *  当前接口  就是  aidl接口 
 *  在该接口中需要定义供客户端访问的抽象方法
 */
interface MusicInterface {
    void play();
    void next();
    void pre();
}

点击进入根文件自动生成的文件:
这里写图片描述
3、在清单文件中注册

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       <service android:name="com.example.android_day11_service.PlayMusicService">
          <intent-filter >
              <action android:name="com.tarena.action.MUSIC_SERVICE"/>
          </intent-filter> 
       </service>

    </application>

</manifest>

4、运行安装在手机上

客户端

android_day11_phone
1、新建Activity(PlayMusicActivity.java)

package com.example.android_day11_phone;

import com.example.android_day11_service.MusicInterface;
import com.example.android_day11_service.MusicInterface.Stub;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;

public class PlayMusicActivity extends Activity {
    private ServiceConnection conn;
    private MusicInterface musicInterface;

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

        // 执行service绑定
        Intent intent = new Intent("com.tarena.action.MUSIC_SERVICE");
        conn = new ServiceConnection() {
            public void onServiceDisconnected(ComponentName name) {
            }

            // 绑定成功时执行
            public void onServiceConnected(ComponentName name, IBinder service) {
                musicInterface = Stub.asInterface(service);
            }
        };
        this.bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unbindService(conn);
    }

    public void doClick(View view) {
        try {
            switch (view.getId()) {
            case R.id.button1:
                musicInterface.play();
                break;
            case R.id.button2:
                musicInterface.next();
                break;
            case R.id.button3:
                musicInterface.pre();
                break;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.play_music, menu);
        return true;
    }

}

2、客户端想要用,要将服务端AIDL文件复制到客户端(包名一致)
这里写图片描述

3、运行

代码下载:http://download.csdn.net/detail/prince77qiqiqq/9628751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值