定义:接口描述型语言。一种跨进程的通信机制,可以使一个进程提供方法暴露给其他应用来使用
adil:分为Server端和Client端
下面介绍一下Server端的使用步骤
第一步:(1)创建一个.aidl文件在src工程目录
创建文件MyAdil.adil
// MyAdil.aidl
package com.gdwanlian.adilserverdemo;
// Declare any non-default types here with import statements
interface MyAdil {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
String getString();
int getInt();
void start();
}
里面定义了3个方法getInt()、start(),getString();
第二步:
写一个 MyServer类继承Server,重写里面的方法
package com.gdwanlian.adilserverdemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MyService extends Service {
private String myService = "";
@Override
public IBinder onBind(Intent intent) {
return bind;
}
public MyAdil.Stub bind = new MyAdil.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getString() throws RemoteException {
// TODO Auto-generated method stub
Log.e("MyService", "MyService+getString");
return myService;
}
@Override
public int getInt() throws RemoteException {
// TODO Auto-generated method stub
return 1900;
}
@Override
public void start() throws RemoteException {
// TODO Auto-generated method stub
new MyThread().start();
}
};
private class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
URL url = new URL(
"http://litchiapi.jstv.com/api/GetFeeds?column=8&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
InputStream is = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader buffer = new BufferedReader(reader);
String cache;
String str = "";
while ((cache = buffer.readLine()) != null) {
str += cache;
}
Log.e("str", str);
myService = str;
Intent intent = new Intent("com.gz14");
intent.putExtra("txt", myService);
sendBroadcast(intent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在AndroidManifest里面添加权限和注册server
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gdwanlian.adilserverdemo" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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.gz14"/>
</intent-filter>
</service>
</application>
</manifest>
这样Server端的demo创建成功
接下来就是Client端
把Server端的adil文件复制粘贴到新创建的Client端demo里面这个目录下面
直接复制到这里就好,不要改任何东西,接下来就是如何调用Server端那里的3个方法了
(1)在Activity中使用ServiceConnection,重写onServiceConnected方法,获得IBinder的写法如下:iRemoteService = IRemoteService.Stub.asInterface(service)
(2)绑定Service
案例代码如下:
package com.gdwanlian.clientdemo;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.gdwanlian.adilserverdemo.MyAdil;
public class MainActivity extends ActionBarActivity {
MyAdil aidl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("com.gz14");
registerReceiver(new MyBroadcast(), filter);
}
private class MyBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String txt = intent.getStringExtra("txt");
Log.e("onReceive", "onReceive--client" + txt);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent("com.gz14");
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
aidl = MyAdil.Stub.asInterface(service);
try {
aidl.start();
// Log.e(">>>", );
Log.e(">>>", aidl.getInt() + "");
// new MyThread().start();
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
bindService(intent, conn, BIND_AUTO_CREATE);
return super.onOptionsItemSelected(item);
}
}
下面我把demo上传到这个地址,需要的可以看一下点击打开链接