24 服务AIDL
2016-09-23 17:34
232 查看
AIDL远程访问服务的方法
创建一个接口类写上方法 然后修改后缀java为aidl
在服务中创建一个类继承Stub类
在远程访问服务的进程把AIDL文件复制(包名不能改变)
XXXX.Stub.asInterface(service);即可
MainActivity.java
MyService.java
MyInterface.aidl
清单文件:
MainActivity.java
MyInterface.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(); }
相关文章推荐
- 24 服务AIDL
- 24_aidl绑定远程服务
- 简单的android进程间(远程)服务通信示例【AIDL方式】
- 使用AIDL和远程服务实现进程通信
- 在看不见的地方,AI正在7×24为你在线服务
- Android Service学习之AIDL, Parcelable和远程服务
- Service跨进程调用服务三部曲之AIDL详解(二)
- Android四大组件之Service(二)远程服务(AIDL进程间通信)
- Service的使用(四)前台服务与AIDL
- 四大组件之——server(服务)及Aidl(接口定义语言)
- Android 使用【AIDL】调用外部服务
- 我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- Android Service学习之AIDL, Parcelable和远程服务
- 跨进程调用Service(AIDL服务)
- Android服务之Service(其二)关于AIDL进程间通信
- Android Service学习之AIDL, Parcelable和远程服务
- 关于service:本地服务 与 远程服务AIDL
- Android服务之Service——关于AIDL进程间通信(转)
- 我的Android进阶之旅------>Android Service学习之AIDL, Parcelable和远程服务