您的位置:首页 > 大数据 > 人工智能

24 服务AIDL

2016-09-23 17:34 232 查看
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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: