启动其他App的服务,跨进程启动服务。

与启动本应用的Service一样,使用startService(intent)方法

不同的是intent需要携带的内容不同,需要使用intent的setComponent()方法。

setComponent()方法需要传入两个参数,第一个参数是包名,第二个参数是组件名。即,第一个参数传入要启动的其他app的包名,第二个参数传入的时候要启动的其他app的service名。

看下面的例子:(aidlserviceapp应用通过button启动aidlservice应用MyService

aidlserviceapp应用:

 package com.example.aidlserviceapp;

 import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btn_StartService, btn_StopService;
Intent serviceIntent = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent();
ComponentName componentName = new ComponentName(
"com.example.aidlservice", "com.example.aidlservice.MyService");
serviceIntent.setComponent(componentName); // 使用intent的setComponent()方法,启动其他应用的组件。 btn_StartService = (Button) findViewById(R.id.btn_StartService);
btn_StopService = (Button) findViewById(R.id.btn_StopService); btn_StartService.setOnClickListener(this);
btn_StopService.setOnClickListener(this); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_StartService:
startService(serviceIntent);
break;
case R.id.btn_StopService:
stopService(serviceIntent);
break;
default:
break;
}
}
}

aidlservice应用

 package com.example.aidlservice;

 import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service {
public static final String TAG = "aidlservice"; @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "其他应用服务启动");
} @Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "其他应用服务停止");
} }

同时添加MyService在该应用的manifest。

1.启动aidlservice应用

2.启动aidlserviceapp应用

3.点击button,查看打印的log,是否启动了aidlservice应用的MyService服务。

结果如下图,启动成功。

Android开发之AIDL的使用一--跨应用启动Service的更多相关文章

  1. Android开发之Java集合类性能分析

    对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...

  2. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  3. Android开发之Git配置

    Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...

  4. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  5. Android开发之旅: Intents和Intent Filters(理论部分)

    引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...

  6. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  7. Android开发之Java必备基础

    Android开发之Java必备基础 Java类型系统 Java语言基础数据类型有两种:对象和基本类型(Primitives).Java通过强制使用静态类型来确保类型安全,要求每个变量在使用之前必须先 ...

  8. Android开发之PopupWindow

      /* *  Android开发之PopupWindow * *  Created on: 2011-8-8 *  Author: blueeagle *  Email: liujiaxiang@g ...

  9. [置顶] Android开发之MediaPlayerService服务详解(一)

    前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...

随机推荐

  1. jdbc学习(一)——SqlServer、Oracle和MySQL

    一.jdbc介绍 jdbc全称:java数据库连接(Java Database Connectivity),是sun公司定义的一套访问数据库的规范(接口和类,由各种数据库公司进行实现),主要放在jav ...

  2. ItemsControl 使用Grid布局

    ItemsControl控件经常用到,在ItemsPanel里大多是StackPanel,WrapPanel,以下项目演示如何使用Grid用于ItemsControl布局 1.先看运行效果 2.xam ...

  3. 在后台对GameObject进行"创建"||"删除"动作

    在后台对GameObject进行"创建"||"删除"动作 建立 public GameObject Pre;//在编辑器中用来绑定的Prefabs public ...

  4. SQL优化的四个方面,缓存,表结构,索引,SQL语句

    一,缓存 数据库属于 IO 密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所 ...

  5. 分享自lordinloft 《[转载]COMPILE_OPT 的用法介绍》

    来源:http://blog.sina.com.cn/s/blog_63180b75010117oj.html#bsh-73-372143085

  6. iframe 刷新

    iframe刷新父页面 parent.location.reload(); iframe 一个子页面操作过后,刷新指定子页面 parent.frames('ifrmname').location.re ...

  7. redis缓存的安装和使用

    Redis介绍    Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库,但是他的数据可以持久化的保存在磁盘上,解决了服务重启后数据不丢失的问题,他的值可以是s ...

  8. Experience all that SharePoint 15 has to offer. Start now or Remind me later.

    $spSite = Get-SpSite($waUrl); $spSite.AllowSelfServiceUpgrade = $false

  9. H2嵌入式数据库的各种连接方式

    H2database是一款用java语言编写的开源数据库, 一般用作游戏的数据存储, 当然web项目也是可以用的, web项目也可以将该数据库 首先要安装H2数据库 http://www.h2data ...

  10. [SQL SERVER系列]存储过程,游标和触发器实例[原创]

    自己写的存储过程与游标结合使用的实例,与大家分享,也供自己查阅,仅供参考: --使用游标循环处理,删除重复的记录 declare @UserID int ) ) declare @UnitFlag i ...