AIDL的使用

注意点:
1.service端和client端的aidl文件所在的包名必须相同;
2.在service中实现aidl类中的Stub类。

1.两个应用的目录关系:
这里写图片描述

2.AndroidMenifest.xml
服务端:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pax.aidl.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.pax.aidl.service.RemoteService">
            <intent-filter>
                <action android:name="com.pax.aidl.service.remote">
                </action>
            </intent-filter>
        </service>
    </application>

</manifest>

客户端:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pax.aidl.client"
    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" >
        <activity
            android:name="com.pax.aidl.client.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3.aidl文件:

package com.pax.aidl.service;

interface Remote {
    String getUrl();
}

4.service类:

package com.pax.aidl.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.pax.aidl.service.Remote;

public class RemoteService extends Service{

    private final static String TAG = "RemoteService";
    private MyBind myBind = null;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG,"onBind");
        return myBind;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i(TAG,"onCreate");
        myBind = new MyBind();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG,"onDestroy");
    }

    private class MyBind extends Remote.Stub{
        @Override
        public String getUrl() throws RemoteException {
            // TODO Auto-generated method stub
            return "www.baidu.com";
        };
    }

}

5.client Activity类:

package com.pax.aidl.client;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.util.Log;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import com.pax.aidl.service.Remote;

public class MainActivity extends Activity implements OnClickListener{

    private final static String TAG = "MainActivity";
    private Button bt = null;
    private TextView tv = null;
    private Remote remote = null;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"onCreate");

        bt = (Button)findViewById(R.id.geturl_bt);
        tv = (TextView)findViewById(R.id.url_text);
        bt.setOnClickListener(this);

        Intent intent  = new Intent("com.pax.aidl.service.remote");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i(TAG,"onClick");
        try {
            url = remote.getUrl();
            tv.setText(url);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG,"onDestroy");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.i(TAG,"onPause");
        unbindService(mConnection);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.i(TAG,"onStop");
    }

    private ServiceConnection mConnection = new ServiceConnection(){


        @Override
        public void onServiceConnected(ComponentName arg0, IBinder service) {
            // TODO Auto-generated method stub
            Log.i(TAG,"connect service");
            remote = Remote.Stub.asInterface(service);
            if(remote == null){
                Log.e(TAG,"remote is null");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            // TODO Auto-generated method stub
            remote = null;
        }
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值