在进程间通信时,常会设计开启远程 Service 的情况。开启远程 Service 的方式有两种,一种时显示开启,一种是隐式开启。下面分别来看:

一、隐式开启

  服务端:Service 所在 AndroidManifest.xml 中的配置如下,注意 exported = "true" ,为 true 才可被其他 App 访问,否则就只限于应用内。

 <service
android:name=".BookManagerService"
android:exported="true">
<intent-filter>
<action android:name="com.sl.aidl"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>

  客户端:需要开启远程服务

Intent service = new Intent();
service.setAction("com.sl.aidl"); //service 的 action 值
service.setPackage("com.sl.binderservice"); //远程服务所在包名
//绑定服务
bindService(service, mConnection, Context.BIND_AUTO_CREATE);
//启动服务
startService(service);

二、显示开启

  服务端:AndroidManifest.xml

<service android:name=".BookManagerService" android:exported="true"/>

  客户端

public static final String NAME_REMOTE_SERVICE = "com.sl.binderservice.BookManagerService" ;
public static final String PACKAGE_REMOTE_SERVICE = "com.sl.binderservice" ;
//启动服务
Intent startIntent = new Intent ();
ComponentName componentName = new ComponentName(PACKAGE_REMOTE_SERVICE ,NAME_REMOTE_SERVICE);
startIntent .setComponent (componentName );
startService( startIntent) ;
//绑定服务
Intent startIntent = new Intent ();
ComponentName componentName = new ComponentName(PACKAGE_REMOTE_SERVICE ,NAME_REMOTE_SERVICE);
startIntent .setComponent (componentName );
bindService( startIntent, mConnection, Context.BIND_AUTO_CREATE) ;

  以上就是这两种开启方式。

远程Service的显示 / 隐式启动的更多相关文章

  1. ANdroid5.0不能隐式启动service,必须显示,解决办法,加服务端包名

    Intent intent = new Intent(); intent.setAction("com.viaembedded.veonvif.RemoteService");// ...

  2. Intent显示启动与隐式启动

    Android的Acitivity启动大致有两种方式:显式启动与隐式启动.下面分别介绍: 1.显示启动: 清单文件注册Activity <activity android:name=" ...

  3. Android隐式启动匹配:action,category,data

    简介 Android开发中,Activity,Service 和 BroadcastReceiver 启动有两种方式,显示启动和隐式启动. 为方便下面描述,我以Activity启动为例. 显示启动便是 ...

  4. Android隐式启动Activity可能存在的坑

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 本篇文章,对隐式启动Activity再做分析. 有些人可能会说了, ...

  5. 第一行代码阅读笔记----显示隐式Intent的基本用法

    1.显示Intent意图明显,通过Intent启动另外一个活动,这是安卓中各组件进行交互的一种重要方式.一般用于启动活动,启动服务,发送广播等场景. 实现方法,这里我只说思路,实践还是要自己实操才能明 ...

  6. 显式启动Activity和隐式启动Activity

    1.显式启动Intent intent = new Intent(this, class);startActivity(intent); 2.隐式启动AndroidManifest.xml中定义某个A ...

  7. Android-隐藏app图标以及隐式启动

    隐藏APP桌面图标 <activity android:name=".LaunchActivity"> <intent-filter> <action ...

  8. Android-----Intent中通过startActivity(Intent intent )隐式启动新的Activity

    显式Intent我已经简单使用过了,也介绍过概念,现在来说一说隐式Intent: 隐式Intent:就是只在Intent中设置要进行的动作,可以用setAction()和setData()来填入要执行 ...

  9. 隐式启动判断是否有匹配的Intent

    一.PackageManager的resolveActivity public abstract ResolveInfo resolveActivity(Intent intent, int flag ...

随机推荐

  1. host元素的属性autoDeploy和reloadable的区别

    web.xml文件的修改会触发AutoDeploy,受host节的autoDeploy配置值的影响. class类文件修改会触发Reload操作,受reloadable配置值的影响. 而autoDep ...

  2. Deep Learning Terminologies

    Deep Learning Terminologies batch full batch 已知在梯度下降中,需要对所有样本进行处理过后然后走一步(梯度更新),那么如果我们的样本规模的特别大的话效率就会 ...

  3. libvirt的security

    1. libvirt支持SASL authentication and encryption MD5 hashes are considered unsafe and should not be us ...

  4. php 输出缓存,每秒打印一个数字

    <?php set_time_limit(0); //以上三行不加上nginx下不执行,一次性显示出来 header('Content-Type: text/event-stream'); // ...

  5. py4CV例子1猫狗大战和Knn算法

    1.什么是猫狗大战: 数据集来源于Kaggle(一个为开发商和数据科学家提供举办机器学习竞赛.托管数据库.编写和分享代码的平台),原数据集有12500只猫和12500只狗,分为训练.测试两个部分. 2 ...

  6. I2C总线的仲裁机制

    在多主的通信系统中.总线上有多个节点,它们都有自己的寻址地址,可以作为从节点被别的节点访问,同时它们都可以作为主节点向其他的节点发送控制字节和传 送数据.但是如果有两个或两个以上的节点都向总线上发送启 ...

  7. Codeforces 903G Yet Another Maxflow Problem - 线段树

    题目传送门 传送门I 传送门II 传送门III 题目大意 给定一个网络.网络分为$A$,$B$两个部分,每边各有$n$个点.对于$A_{i} \ (1\leqslant i < n)$会向$A_ ...

  8. 牛客网数据库SQL实战(11-15)

    11.获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'.结果第一列给出当前员工的emp_no,第二列给出其manager ...

  9. Objective-C 【init/initWithFrame调用机制】

    这是一个自定义view: @implementation MyView - (instancetype)init { if (self = [super init]) { NSLog(@"调 ...

  10. topcoder srm 520 div1

    problem1 link 设$f[i][j][k]$表示考虑了前$i$道题,剩下时间为$j$,剩下技能为$k$的最大得分. 从小到大计算二元组$(j,k)$的话,在存储上可以省略掉$i$这一维. p ...