今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索。

AIDL(Android Interface Definition Language)是Android接口定义语言的意思,它可以用于让某个Service与多个应用程序组件之间进行跨进程通信,从而可以实现多个应用程序共享同一个Service的功能。

言归正传,今天的主题是远程Service建立AIDL进行通信,通过一个小demo来展示AIDL在Android studio中的实现:

1. 搭建了一个简单的Service框架,仅包括startService(intent),框架在后面代码中展示出来

2. 然后建立AIDL,通过点击建立AIDL文件,如图

建立好之后,出现AIDL文件如图

但是此时并没有AIDL的java文件产生,其实android studio也是带有自动生成的,只不过需要确认一些信息后才能生成。此时,我们可以在目录 build-->generated-->source-->aidl-->test-->debug下面发现还没有任何文件

此时,打开AndroidManifest.xml,确认package的值,如我这个

关键性的一步,确认aidl文件所在的包名和AndroidMainifest.xml的package名是否一致。如果一致,点击 Build-->Make Project,生成相应的java文件。如果不一致,则改aidl的包名,改成一致,再点击生成,生成效果如图。

则此时就可以在程序中通过AIDL调用远程Service的方法,实现AIDL与远程Service进行通信,代码展示如下。

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private MyServiceAIDL myServiceAIDL;
private Intent binderIntent;
private final static boolean create_flag=true;
private final static boolean destory_flag=false;
private final static String TAG="MainActivity"; private ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myServiceAIDL = MyServiceAIDL.Stub.asInterface(service);
try {
//通过AIDL远程调用
Log.d(TAG,"++start download++");
myServiceAIDL.DownLoad();
} catch (RemoteException e) {
e.printStackTrace();
}
} @Override
public void onServiceDisconnected(ComponentName name) { }
}; @Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"++MainActivity onCreate++");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //开启服务
Intent intent = new Intent(this, MainService.class);
startService(intent); //连接远程Service和Activity
binderIntent = new Intent(this,MainService.class);
Bundle bundle = new Bundle();
bundle.putBoolean("flag",create_flag);
binderIntent.putExtras(bundle);
bindService(binderIntent, sc, BIND_AUTO_CREATE); } @Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"++MainActivity onDestroy++"); boolean flag = false;
//暂停服务
Intent intent = new Intent(this, MainService.class);
stopService(intent); //断开与远程Service的连接
unbindService(sc);
}
}

MainService.java

public class MainService extends Service {

    boolean flag;
private final static String TAG = "MainService"; @Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "++MainService onDestroy++");
flag = false;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
} @Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "++MainService onCreate++"); Notification no = new Notification(R.drawable.ic_launcher, "有通知到来", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
no.setLatestEventInfo(this, "AIDLDemo", "running", pi);
startForeground(1, no);
} @Override
public IBinder onBind(Intent intent) { Bundle bundle = intent.getExtras();
flag = bundle.getBoolean("flag");
System.out.println(flag);
return ms;
} MyServiceAIDL.Stub ms = new MyServiceAIDL.Stub() {
@Override
public void DownLoad() throws RemoteException { new Thread(new Runnable() {
int i = 0; @Override
public void run() {
//未达到线程条件,会一直在后台运行,就算服务已经关闭
while (flag) { try {
i++;
System.out.println("i的值是" + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("退出服务");
}
}).start(); }
};
}

AndroidManifest.xml

<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=".MainService"
android:process=":remote"></service>
</application>

MyServiceAIDL.aidl

// myServiceAIDL.aidl
package com.example.wanghao.aidldemo; // Declare any non-default types here with import statements interface MyServiceAIDL { void DownLoad();
}

参考文章:http://blog.csdn.net/guolin_blog/article/details/9797169

Android开发爱好者,文中如果有错误之处,望大神们留言指出,相互学习,共同进步!

大仙说道之Android studio实现Service AIDL的更多相关文章

  1. Android Studio实现Service AIDL

    Android Studio实现Service AIDL [日期:2015-01-02] 来源:Linux社区  作者:teenyboy [字体:大 中 小]       今天要开发过程中要用到AID ...

  2. Android studio 中创建AIDL Service

      1.概述  AIDL在android系统中的作用 AIDL,Android Interface definition language的缩写,它是一种android内部进程通信接口的描写叙述语言, ...

  3. Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用

    在上一篇文章Android IPC机制(二)用Messenger进行进程间通信中我们介绍了使用Messenger来进行进程间通信的方法.可是我们能发现Messenger是以串行的方式来处理client ...

  4. Android Studio中实现AIDL

    AIDL 先来两个传送门: http://www.cnblogs.com/yydcdut/p/3961545.html Android面试,与Service交互方式 http://www.cnblog ...

  5. 使用Android studio创建的AIDL编译时找不到自定义类的解决办法

    使用AS创建ADIL文件时AS会在main文件夹下给我们生成一个aidl文件夹和一个相同包名的包,通常我们会把所有和ADIL相关的类或文件放在这个包下,但是如果存在自定义的类时,程序编译时无法通过,提 ...

  6. 为什么 Android Studio 工程文件夹占用空间这么大?我们来给它减减肥

    偶然中发现Android Studio的工程文件夹比ADT Bundle的大很多.用Android Studio新建一个空工程,工程文件夹大小为30M,运行一次后大小为40M.同样用ADT Bundl ...

  7. Android Studio安装、配置

    Google在2013年I/O大会上发布了Android Studio,AndroidStudio是一个基于IntelliJ IDEA的Android开发工具.这个IDE要比eclipse智能很多,具 ...

  8. Android Studio无法启动 打开, Android Studio gradle下载不了

    Google在2013年I/O大会上发布了Android Studio,AndroidStudio是一个基于IntelliJ思想的新的Android开发工具.下面介绍一下Android Studio安 ...

  9. Android Studio安装使用图文教程

    原文 http://jingyan.baidu.com/article/1876c852a25cb4890b1376fa.html Google I/O开发者大会上宣布,Android Studio ...

随机推荐

  1. Working with MTD Devices

    转:http://www.linuxforu.com/2012/01/working-with-mtd-devices/ Working with MTD Devices By Mohan Lal J ...

  2. [Arduino] 在串口读取多个字符串,并且转换为数字数组

    功能如题目.在串口收到逗号分割的6串数字比如100,200,45,4,87,99然后在6个PWM端口3, 5, 6, 9, 10, 11输出对应PWM值代码注释很详细了,就不再说明了. //定义一个c ...

  3. JAVA中的deflate压缩实现

    在文件的传输过程中,为了使大文件能够更加方便快速的传输,一般采用压缩的办法来对文件压缩后再传输,JAVA中的java.util.zip包中的Deflater和Inflater类为使用者提供了DEFLA ...

  4. 把Flume的Source设置为 Spooling directory source

    把Flume的Source设置为 Spooling directory source,在设定的目录下放置需要读取的文件,一些文件在读取过程中会报错. 文件格式和报错如下: 实验一 读取汉子和“:&qu ...

  5. (ASP.NET)总结MVC中@Html表单用法

    1.当type类型是text时:@Html.TextBoxFor(model => Model.Name,new{@style = "width: 50px;", @clas ...

  6. HDU 2845 Beans (DP)

    Beans Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. 看完final的感受

    今天没课,(其实是有体育课的,去打了一会球就跑路了...)就在宿舍看world final ; 我去,老毛子真是好厉害,看的我目瞪口呆,哈喇子直流; 上交的大神好厉害,本来还以为上交要夺冠的,最后罚时 ...

  8. 转: 关于 ssl的建立链接的过程

    转自:  http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html SSL/TLS协议运行机制的概述   作者: 阮一峰 日期: 2014年2月 5日 互 ...

  9. Backbone.js学习之Router

    官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...

  10. asp结合ajax中文乱码问题

    XMLHttpRequest 在w3c标准中这样提到: 如果响应包含了为响应体指定字符编码的头部,就使用该编码.否则,假定使用 Unicode UTF-8. 前端页面sele.asp <&quo ...