在进程间通信时,常会设计开启远程 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. 网上搜到的权限系统demo

    网上搜到的权限系统demo http://www.sojson.com/shiro

  2. linux的/etc/passwd、/etc/shadow、/etc/group和/etc/gshadow—关于用户和组的配置文件

    1./etc/passwd  存储用户信息 [root@oldboy ~]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bi ...

  3. 阿里云Maven settings.xml文件

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  4. flask自动代码自动补全

    编写py文件时,无法补全: 在app对象后面添加:# type:Flask app=Flask(__name__)   # type:Flask from flask import Flask, fl ...

  5. shell脚本一键安装nginx

    依赖包安装包放在一起, 直接执行这个脚本就行. #!/bin/bash #--------------------------------------------------------------- ...

  6. linux下网卡bonding配置(转)

    linux下网卡bonding配置   章节 bonding技术 centos7配置bonding centos6配置bonding 一.bonding技术 bonding(绑定)是一种linux系统 ...

  7. answerOpenCV轮廓类问题解析

    contour在opencv中是一个基础的数据结构,灵活运用的话,作用很大.以contour为关键字,在answerOpenCV中能够发现很多有趣的东西. 1.无法解决的问题 http://answe ...

  8. 线性回归、Logistic回归、Softmax回归

    线性回归(Linear Regression) 什么是回归? 给定一些数据,{(x1,y1),(x2,y2)…(xn,yn) },x的值来预测y的值,通常地,y的值是连续的就是回归问题,y的值是离散的 ...

  9. [内核驱动] miniFilter 内核层与应用程序通信

    转载:http://blog.csdn.net/heyabo/article/details/8721611 转载:http://www.cnblogs.com/ljinshuan/archive/2 ...

  10. shell脚本之 给PNG图片添加后缀@3x

    1,给png图片加上后缀@3x #!/bin/sh #root_src=$(dirname $(PWD)) #echo ${root_src} image_path=${root_src}/image ...