Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)——安卓接口定义语言
一、startService/stopService
1、同一个应用程序启动Service:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this,AppService.class)); //启动Service
}
protected void onDestroy() {
super.onDestroy();
stopService(new Intent(this,AppService.class)); //停止Service
}
2、跨应用启动service:
通过Action的隐式Intent启动Service在安卓5.0以前的版本是可以实现跨应用启动Service的,安卓5.0以后的版本若想实现
跨应用启动Service的功能必须使用显示Intent。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Intent serviceIntent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent();
serviceIntent.setComponent(new ComponentName("com.w.startservicefromanotherapp","com.w.startservicefromanotherapp.AppService"));
findViewById(R.id.btnStartService).setOnClickListener(this);
findViewById(R.id.btnStopService).setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.btnStartService:
// Intent i = new Intent();
//i.setComponent(new ComponentName("com.w.startservicefromanotherapp","com.w.startservicefromanotherapp.AppService"));
//显示 Intent被启动程序的包名,被启动服务的类名
startService(serviceIntent);
break;
case R.id.btnStopService:
stopService(serviceIntent);
break;
}
}
}
二、bindService/unbindService
跨应用绑定Service:AIDL——用于在多个应用程序之间进行通信
1、在StartServiceFromAnotherApp新建AIDL文件IAppServiceRomoteBinder,会全自动生成相关的类。
2、在返回Binder时(AppService):
public IBinder onBind(Intent intent) {
return new IAppServiceRomoteBinder.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }
};
}
3、在AnotherApp项目中绑定这个服务:
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnbindService).setOnClickListener(this);
case R.id.btnBindService:
bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
break;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("Bind Service");
System.out.println(service); //显示IBinder service
}
@Override
public void onServiceDisconnected(ComponentName name) { }
Android中AIDL的理解与使用(一)——跨应用启动/绑定Service的更多相关文章
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...
- Android中AIDL通信机制分析
一.背景 ·1.AIDL出现的原因 在android系统中,每一个程序都是运行在自己的进程中,进程之间无法进行通讯,为了在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需 ...
- Android 中AIDL的使用与理解
AIDL的使用: 最常见的aidl的使用就是Service的跨进程通信了,那么我们就写一个Activity和Service的跨进程通信吧. 首先,我们就在AS里面新建一个aidl文件(ps:现在AS建 ...
- (七)Android中AIDL的应用与理解
一.跨应用启动Service Intent serviceIntent=new Intent();serviceIntent.setComponent(new ComponentName(" ...
- Android中一个经典理解误区的剖析
今天,在Q群中有网友(@广州-包晴天)发出了网上的一个相对经典的问题,问题具体见下图. 本来是无意写此文的,但群里多个网友热情不好推却,于是,撰此文予以分析. 从这个问题的陈述中,我们发现,提问者明显 ...
- 彻底明确Android中AIDL及其使用
1.为什么要有AIDL? 不管学什么东西,最先得弄明确为什么要有这个东西.不要说存在即是合理.存在肯定合理,可是你还是没有明确. 对于AIDL有一些人的浅显概念就是,AIDL能够跨进程訪问其它应用程序 ...
- 【转】android中layout_weight的理解
android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...
- (四)Android中Context的理解与使用
一.Context的作用 Context可用于访问全局资源. public class MainActivity extends Activity { private TextView tv; @Ov ...
- Android中Context的理解及使用(二)——Application的用途和生命周期
实现数据共享功能: 多个Activity里面,可以使用Application来实现数据的共享,因为对于同一个应用程序来说,Application是唯一的. 1.实现全局共享的数据App.java继承自 ...
随机推荐
- QWhatsThis的用法
QWhatsThis,为任何widget提供简单的描述,回答"What's This?"这个问题. 示例:(在MainWindow下运行) 定义全局变量 QAction *newA ...
- laravel框架中容器类简化代码-摘自某书
<?php //容器类装实例或提供实例的回调函数 class Container { protected $bindings = []; //绑定接口和生成相应实例的回调函数 public fu ...
- 使用EditText的addTextChangedListener(new TextWatcher())方法
(转:http://www.apkbus.com/android-5257-1-14.html) 在使用EditText的addTextChangedListener(new TextWatcher( ...
- SQL 中的 AND OR
AND 和 OR 运算符用于基于一个以上的条件对记录进行过滤. AND 和 OR 运算符 AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来. 如果第一个条件和第二个条件都成立,则 ...
- MySQL安装与基本配置
一.简介 SQL语言 DDL:表.视图.索引.触发器操作等.CREATE/ALTER/DROP语句 DML:数据操作.SELECT/INSERT/UPDATE/DELETE DCL:权限设置.GRAN ...
- 为什么 C++ 中成员函数指针是 16 字节?
当我们讨论指针时,通常假设它是一种可以用 void * 指针来表示的东西,在 x86_64 平台下是 8 个字节大小.例如,下面是来自 维基百科中关于 x86_64 的文章 的摘录: Pushes a ...
- LAMP环境配置 linux+apache+mysql+php
虚拟机安装Linux系统: 新建虚拟机过程中选择Linux,下面选择centos或者是Ubuntu Linux切换图像命令:注意只有装了图像界面才可以切换 查看安装环境的版本: rpm -qa 查看安 ...
- bootstrap自学总结不间断更新
2.栅格系统 container-fluid 自适应宽度100% container 固定宽度(适应响应式) 屏幕宽度=x x>=1200 1170 992< ...
- Django基础之安装配置
安装配置 一 MVC和MTV模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象与数据库的 ...
- 【原】Spark之机器学习(Python版)(一)——聚类
kmeans聚类相信大家都已经很熟悉了.在Python里我们用kmeans通常调用Sklearn包(当然自己写也很简单).那么在Spark里能不能也直接使用sklean包呢?目前来说直接使用有点困 ...