AIDL初步
来源:互联网 发布:奔富bin2 知乎 编辑:程序博客网 时间:2024/06/06 19:42
一、整体工程图
二、首先创建在两个工程中都创建RemoteWebPage.aidl,然后分别在gen目录下生成了RemoteWebPage.java
package com.braincol.aidl.service; interface RemoteWebPage {String getCurrentPageUrl(); }
三、在RemoteService实现接口,并且在onBind方法中返回IBinder对象。
package com.braincol.aidl.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;/** * * @author chenzheng_java * @description 提供服务的service * */public class RemoteService extends Service {private final static String TAG = "RemoteService";@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "执行了OnBind");return new MyBinder();}private class MyBinder extends RemoteWebPage.Stub{@Overridepublic String getCurrentPageUrl() throws RemoteException{return "http://www.cnblogs.com/hibraincol/";}}}
四、ClientActivity中通过remoteWebPage = RemoteWebPage.Stub.asInterface(service)获取到RemoteWebPage,就可以调用其中的方法getCurrentPageUrl了。
package com.braincol.aidl.client;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.braincol.aidl.service.RemoteWebPage;public class ClientActivity extends Activity implements OnClickListener {private final static String TAG="jltxgcy";Button btn_bind ;String actionName = "com.braincol.aidl.remote.webpage";RemoteWebPage remoteWebPage=null;String allInfo = null;boolean isBinded=false;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn_bind = (Button) findViewById(R.id.btn_bind);btn_bind.setOnClickListener(this);}@Overrideprotected void onPause(){super.onPause();if(isBinded){Log.d(TAG,"unbind");unbindService(connection);}}private class MyServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i(TAG, "建立连接...");remoteWebPage = RemoteWebPage.Stub.asInterface(service);try {Log.d(TAG, remoteWebPage.getCurrentPageUrl());} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, "onServiceDisconnected...");}}MyServiceConnection connection = new MyServiceConnection();@Overridepublic void onClick(View v) {if(v==this.btn_bind){if(!isBinded){Intent intent = new Intent(actionName);bindService(intent, connection, Context.BIND_AUTO_CREATE);isBinded= true;}}}}
五、另外注意RemoteService的AndroidManifest.xml写法。
ClientActivity的AndroidManifest.xml为:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.braincol.aidl.client" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ClientActivity" 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>
RemoteService的AndroidManifest.xml为:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.braincol.aidl.service" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"><service android:name="RemoteService"><intent-filter><action android:name="com.braincol.aidl.remote.webpage"/></intent-filter></service> </application></manifest>
代码地址:https://github.com/jltxgcy/Demo
- AIDL初步
- aidl初步学习
- AIDL初步了解
- 初步理解android中的aidl
- AIDL——Android接口定义语言初步探索
- AIDL
- aidl
- AIDL
- AIDL
- AIDL
- AIDL
- AIDL
- aidl
- aidl
- AIDL
- AIDL
- aidl
- AIDL
- c语言虽难,但是只要入门了,学习了那个思想后,就觉得容易多了!
- sublime text 运行python代码,图形无显示
- mysql数据库备份与还原
- 内链
- 2013资格赛——桃花群岛
- AIDL初步
- 盒子游戏解题报告
- Java基础<十二>--->集合之map
- 大型商业网站的架构--淘宝网
- KSFetch Annoyance on Mac OS X 10.8 ML with ‘Hands Off’ or ‘Little Snitch’ Firewall.
- centos 6.4 更新源地址
- 动态规划——矩阵链相乘
- php文件的一些教程
- 根据jquery-file-upload简单写的一个工程(针对之前的简单实用指南)