首页 技术 正文
技术 2022年11月16日
0 收藏 642 点赞 2,316 浏览 3078 个字

AIDL远程访问服务的方法

  • 创建一个接口类写上方法 然后修改后缀java为aidl
  • 在服务中创建一个类继承Stub类
  • 在远程访问服务的进程把AIDL文件复制(包名不能改变)

    XXXX.Stub.asInterface(service);即可


服务端结构

MainActivity.java

无实际代码

MyService.java

package com.qf.day24_aidl_server;import com.qf.day24_aidl_server.MyInterface.Stub;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;public class MyService extends Service{ @Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
//Stub extends android.os.Binder implements com.qf.day24_aidl_server.MyInterface
public class MyBinder extends Stub{ @Override
public String getServiceData() {
return name();
} } //服务里的方法
public String name(){
return "田师傅红烧肉";
} @Override
public void onCreate() {
super.onCreate();
} @Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
} @Override
public void onDestroy() {
super.onDestroy();
}}

MyInterface.aidl

package com.qf.day24_aidl_server; interface MyInterface {     String getServiceData();}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.day24_aidl_server"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.day24_aidl_server.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> <service android:name=".MyService">
<intent-filter >
<action android:name="com.qf.day24_aidl_server.MyService"/>
</intent-filter>
</service>
</application></manifest>

客户端

MainActivity.java

package com.qf.day24_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 com.qf.day24_aidl_server.MyInterface;public class MainActivity extends Activity { private MyInterface myInterface; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Intent intent = new Intent("com.qf.day24_aidl_server.MyService");
//6.0必须设置
intent.setPackage("com.qf.day24_aidl_server"); bindService(intent, new MyServiceConn(), Context.BIND_AUTO_CREATE); } //点击 获取Service里数据
public void MyClick(View v){
String name;
try {
name = myInterface.getServiceData();
Log.e("AAA", "===>"+name);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public class MyServiceConn implements ServiceConnection{ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myInterface = MyInterface.Stub.asInterface(service);
//myInterface = myBinder;
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub } }
}

MyInterface.aidl

package com.qf.day24_aidl_server; interface MyInterface {     String getServiceData();}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289