Android Studio实现Service AIDL

[日期:2015-01-02] 来源:Linux社区  作者:teenyboy [字体:  ]
 
 
 

今天要开发过程中要用到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();
}

Ubuntu 12.04(64位)安装Android Studio 全过程 http://www.linuxidc.com/Linux/2013-05/84812.htm

Android Studio v0.1尝鲜 http://www.linuxidc.com/Linux/2013-05/84681.htm

Android Studio使用教程 http://www.linuxidc.com/Linux/2013-05/84579.htm

Android Studio开发指南 http://www.linuxidc.com/Linux/2013-05/84543.htm

Android Studio设置主题 和 不支持中文的问题解决方法 http://www.linuxidc.com/Linux/2013-05/84488.htm

Android Studio 下载安装以及不能打开的解决���法 http://www.linuxidc.com/Linux/2013-05/84409.htm

Android Studio安装使用图文教程 http://www.linuxidc.com/Linux/2014-09/106914.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-01/111148.htm

 
 
 

Android Studio实现Service AIDL的更多相关文章

  1. 大仙说道之Android studio实现Service AIDL

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

  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 Service AIDL 详解

    公司产品之前IM这块存在很多问题,消息到达率低,加上协议上有些问题,丢消息频繁,所以需要重构IM,AIDL不能解决以上问题.好吧!那AIDL可以解决什么问题?什么是AIDL? 什么是AIDL? AID ...

  7. android studio 使用 aidl(三)权限验证

    这篇文章是基于android studio 使用 aidl (一) 和 android studio 使用 aidl(二) 异步回调 下面的代码都是简化的,如果看不懂请先移步上2篇文章 网上的东西太坑 ...

  8. android studio 使用 aidl(二)异步回调

    基础使用请移步 android studio 使用 aidl (一) 首先建立在server端建立两个aidl文件 ITaskCallback.aidl 用于存放要回调client端的方法 // IT ...

  9. android studio 使用 aidl(一)基础用法

    最近公司需要开发一个项目用的到aidl,之前研究过eclipse版本的,但是好久了一直没用,现在需要捡起来,但是现在都用android studio了,所以查了下资料 都不是很全,我在这里总结一下,方 ...

随机推荐

  1. nodejs全局安装与本地安装区别

    本地安装 1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modul ...

  2. linux和win7设置静态ip

    ubuntu 静态ip设置 检查网络ifconfig (不是ipconfig)必须有2个地址一个回送地址:127.0.0.1一个实际地址:192.168.3.58 sudo vim /etc/netw ...

  3. HDU1379:DNA Sorting

    Problem Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries ...

  4. DLL调试方法

    1.已经做好的dll不能设置:你可以用AfxMessageBox把信息打印出来.2.哪个地方调用的函数 把DLL重新编译一次 在把DLL放到工程里 从新添加一下 然后在你工程调用DLL内容的地方设置断 ...

  5. Golang: pprof

    压测的时候,如果在应用包里加入runtime包,会对压测产生非常严重的干扰. 测试1:开启runtime包 [luwenwei@test-weishi01v ~]$ siege -c --time=1 ...

  6. 初识Iaas,paas

    Iaas(Infrastructure-as-a-service),直译为基础设备作为一种服务. Paas(Platform as a service),直译为平台作为一种服务. 暂且忘掉这两个单词, ...

  7. Docker私有仓库Registry 搭建

    1. 关于Registry 官方的Docker hub是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去.但是,有时候,我们的使用场景需要我们拥有一个私有 ...

  8. 更改 android realtek的系统权限

    由于在 在删除系统的文件时候出现 Read-only file system,所以要获取权限. 推出shell adb mount mount -o rw,remount /system 就可以了

  9. string.IsNullOrWhiteSpace

    string.IsNullOrWhiteSpace(str) 这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char. ...

  10. git 使用系列(二)---- 分支和合并

    Branching and Merging The Git feature that really makes it stand apart from nearly every other SCM o ...