Activity与Service通信之AIDL远程服务
2016-06-01 16:45
363 查看
一、原理
http://note.youdao.com/yws/public/redirect/share?id=dbfce2a37b24ddc0ffff4722221e0e65&type=false
其实简单来说,AIDL可以实现应用程序之间进行通信。
二、使用Android studio创建AIDL
如何创建可以查看《Android Studio 创建AIDL》,这里就不再累述了。
其中:AIDL文件
注意:
Service 和Client中AIDL文件所在的包名必须一致,记得创建后要记得Rebuild 一下,不然没有办法使用;
Rebuild后,系统会自动生成类似xxx.Stub的类。
这样我们就可以在Service端使用了如下:
在Client端使用如下:
二、使用隐式意图启动服务
在Service端我们注册服务是这样的:
在client端启动该服务时,我们需要action如下:
但是,在这里有个问题就是Android 5.0以后不支持隐式意图启动服务(会报参数异常)。那么我们怎么才能够启动该远程服务?
解决方案,使用如下的方法把隐式意图转为显示意图,然后才启动服务就好了。
三、核心代码:
Service:
其中MainActivity没有处理,记得要先运行该程序才可以。
Client:
源码下载地址:
http://download.csdn.net/detail/lu1024188315/9537991
效果图:
http://note.youdao.com/yws/public/redirect/share?id=dbfce2a37b24ddc0ffff4722221e0e65&type=false
其实简单来说,AIDL可以实现应用程序之间进行通信。
二、使用Android studio创建AIDL
如何创建可以查看《Android Studio 创建AIDL》,这里就不再累述了。
其中:AIDL文件
package testndk.testserviceaidl; // Declare any non-default types here with import statements interface ICountService { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ int getCount(); }
注意:
Service 和Client中AIDL文件所在的包名必须一致,记得创建后要记得Rebuild 一下,不然没有办法使用;
Rebuild后,系统会自动生成类似xxx.Stub的类。
这样我们就可以在Service端使用了如下:
class AIDLServerBinder extends ICountService.Stub{ @Override public int getCount() throws RemoteException { return i; } }
在Client端使用如下:
private ServiceConnection serConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { iCountService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { iCountService = ICountService.Stub.asInterface(service); } };
二、使用隐式意图启动服务
在Service端我们注册服务是这样的:
<service android:name="AIDLServer" android:process=":remote"> <intent-filter> <action android:name="testndk.testserviceaidl.AIDLServer" /> </intent-filter> </service>
在client端启动该服务时,我们需要action如下:
Intent intent = new Intent("testndk.testserviceaidl.AIDLServer");
但是,在这里有个问题就是Android 5.0以后不支持隐式意图启动服务(会报参数异常)。那么我们怎么才能够启动该远程服务?
解决方案,使用如下的方法把隐式意图转为显示意图,然后才启动服务就好了。
/*** * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent, * "java.lang.IllegalArgumentException: Service Intent must be explicit" * * If you are using an implicit intent, and know only 1 target would answer this intent, * This method will help you turn the implicit intent into the explicit form. * * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466 * @param context * @param implicitIntent - The original implicit intent * @return Explicit Intent created from the implicit original intent */ public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; }
三、核心代码:
Service:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class AIDLServer extends Service {
private static final int TIME = 1;
private Timer mTimer = null;
private int i = 0;
private AIDLServerBinder serviceBinder = new AIDLServerBinder();
class AIDLServerBinder extends ICountService.Stub{ @Override public int getCount() throws RemoteException { return i; } }
@Override
public void onCreate() {
super.onCreate();
mTimer = new Timer();
mTimer.schedule(new MyTimerTask(), 0,TIME * 1000);
}
@Override
public void onDestroy() {
super.onDestroy();
if(mTimer!=null){
mTimer.cancel();
mTimer = null;
}
}
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
class MyTimerTask extends TimerTask{
@Override
public void run() {
if(i==100){
i=0;
}
i++;
}
}
}
其中MainActivity没有处理,记得要先运行该程序才可以。
Client:
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import testndk.testserviceaidl.ICountService;
public class AIDLClientActivity extends Activity {
private static final int TIME = 1;
private Button startBtn = null;
private Button stopBtn = null;
private TextView mTextView = null;
private ProgressBar mProgressBar = null;
private boolean mIsBind;
private Timer mTimer = null;
/**获取ICountService实例*/
private ICountService iCountService = null;
/**开启服务的显示意图*/
private Intent eintent;
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
int count = iCountService.getCount();
mTextView.setText(count+"%");
mProgressBar.setProgress(count);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
private ServiceConnection serConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { iCountService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { iCountService = ICountService.Stub.asInterface(service); } };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimer = new Timer();
Intent intent = new Intent("testndk.testserviceaidl.AIDLServer");
//把隐式意图转为显示意图
eintent = new Intent(createExplicitFromImplicitIntent(this,intent));
mTextView = (TextView)findViewById(R.id.loading_Tv);
mProgressBar = (ProgressBar)findViewById(R.id.myProgressBar);
mProgressBar.setMax(100);
startBtn = (Button)findViewById(R.id.start_Btn);
stopBtn = (Button)findViewById(R.id.stop_Btn);
startBtn.setOnClickListener(new ButtonClickListener());
stopBtn.setOnClickListener(new ButtonClickListener());
}
class ButtonClickListener implements OnClickListener{
@Override
public void onClick(View v) {
if(startBtn==v){
mIsBind = bindService(eintent, serConn, BIND_AUTO_CREATE);
if (mTimer == null){
mTimer = new Timer();
}
mTimer.schedule(new MyTimerTask(), 1000 ,TIME * 1000);
}else if (stopBtn==v) {
if(mIsBind){
unbindService(serConn);
if(mTimer!=null){
mTimer.cancel();
mTimer = null;
}
mIsBind = false;
}
}
}
}
class MyTimerTask extends TimerTask{
@Override
public void run() {
mHandler.sendMessage(mHandler.obtainMessage());
}
}
/*** * Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent, * "java.lang.IllegalArgumentException: Service Intent must be explicit" * * If you are using an implicit intent, and know only 1 target would answer this intent, * This method will help you turn the implicit intent into the explicit form. * * Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466 * @param context * @param implicitIntent - The original implicit intent * @return Explicit Intent created from the implicit original intent */ public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; }
@Override
protected void onDestroy() {
super.onDestroy();
if(mTimer!=null){
mTimer.cancel();
mTimer = null;
}
if (mIsBind){
unbindService(serConn);
}
}
}
源码下载地址:
http://download.csdn.net/detail/lu1024188315/9537991
效果图:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories