Android之AIDL入门示例

简介: 做了一个小例子: TestAIDLServer.apk是AIDL文件的服务端 TestAIDLProxy.apk是客户端 代码如下 TestAIDLServer中 package com.
做了一个小例子:
TestAIDLServer.apk是AIDL文件的服务端 TestAIDLProxy.apk是客户端
代码如下

TestAIDLServer中

package com.test.aidl;

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

public class MyService extends Service {
	public class MyServiceImpl extends IMyService.Stub {
		public String getValue() throws RemoteException {
			return "Android 链接成功";
		}
	}

	public IBinder onBind(Intent intent) {
		return new MyServiceImpl();
	}
}


IMyService.aidl中
package com.test.aidl;
interface IMyService
{
   String getValue();
}

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.test.aidl" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".TestAIDLServer" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	<service android:name=".MyService">
		<intent-filter>
			<action android:name="com.test.aidl.IMyService" />
		</intent-filter>
	</service>
	</application>
</manifest>


TestAIDLProxy.apk中
吧TestAIDLServer中自动生成的IMyService.java连同包名一起考过来!
TestAIDLProxy.java
package com.test.proxy;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.test.aidl.IMyService;

public class TestAIDLProxy extends Activity implements OnClickListener {

	private IMyService myService = null;
	private Button mButton1;
	private Button mButton2;
	private TextView textView;

	private ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			// 获得服务对象
			myService = IMyService.Stub.asInterface(service);
			mButton2.setEnabled(true);
		}

		public void onServiceDisconnected(ComponentName name) {
		}
	};

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button01:
			textView.setText(":-)");

			// 绑定AIDL服务
			bindService(new Intent("com.test.aidl.IMyService"),
					serviceConnection, Context.BIND_AUTO_CREATE);
			break;
		case R.id.button02:
			try {
				textView.setText(myService.getValue()); // 调用服务端的getValue方法
			} catch (Exception e) {
			}
			break;
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		mButton1 = (Button) findViewById(R.id.button01);
		mButton1.setOnClickListener(this);
		mButton2 = (Button) findViewById(R.id.button02);
		mButton2.setOnClickListener(this);
		mButton2.setEnabled(false);
		textView = (TextView) findViewById(R.id.mTextView01);

	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<Button android:text="@string/buttonstr1" android:id="@+id/button01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	<Button android:text="@string/buttonstr2" android:id="@+id/button02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	<TextView android:layout_width="fill_parent" android:id="@+id/mTextView01"
		android:layout_height="wrap_content" android:text="@string/textviewstr" />
</LinearLayout>




目录
相关文章
|
3月前
|
存储 算法 开发工具
OpenCV 安卓编程示例:1~6 全
OpenCV 安卓编程示例:1~6 全
56 0
|
4月前
|
数据库 Android开发 开发者
Android Studio入门之内容共享ContentProvider讲解以及实现共享数据实战(附源码 超详细必看)
Android Studio入门之内容共享ContentProvider讲解以及实现共享数据实战(附源码 超详细必看)
42 0
|
4月前
|
存储 XML Android开发
Android Studio App开发入门之数据存储中共享参数SharedPreferneces的讲解及使用(附源码 超详细必看)
Android Studio App开发入门之数据存储中共享参数SharedPreferneces的讲解及使用(附源码 超详细必看)
31 0
|
4月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
35 0
|
4月前
|
XML 监控 Android开发
Android Studio App开发入门之文本输入EditText的讲解及使用(附源码 包括编辑框、焦点变更监听器、文本变化监听器 )
Android Studio App开发入门之文本输入EditText的讲解及使用(附源码 包括编辑框、焦点变更监听器、文本变化监听器 )
114 0
|
4月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
90 0
|
1月前
|
测试技术 API 调度
【Android 从入门到出门】第七章:开始使用WorkManager
【Android 从入门到出门】第七章:开始使用WorkManager
20 3
【Android 从入门到出门】第七章:开始使用WorkManager
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
38 3
|
1月前
|
Android开发
【Android 从入门到出门】第四章:现代Android开发中的导航
【Android 从入门到出门】第四章:现代Android开发中的导航
22 2
【Android 从入门到出门】第四章:现代Android开发中的导航
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4