启动其他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服务。

结果如下图,启动成功。

最新文章

  1. 关于datagridview里使用combox的总结
  2. there are no packages available for installation插件安装问题和如何配置浏览器的快捷键
  3. 开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供) 【转】
  4. Android View坐标Left, Right, Top, Bottom
  5. (LinkedList)Intersection of Two Linked Lists
  6. JavaScript学习总结【8】、面向对象编程
  7. LightOj_1030 Discovering Gold
  8. Mac下面的SecureCRT以及破解方案详解
  9. CodeForces 1151E Number of Components
  10. drf视图组件、认证组件
  11. Java 多线程之Timer与ScheduledExecutorService
  12. java字符串根据正则表达式让单词首字母大写
  13. rm 命令
  14. Flask中的模板语言jinja2
  15. Java各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分
  16. 使用HTML5的两个api,前端js完成图片压缩
  17. debian下没有公钥解决办法
  18. wxpython Menus and toolbars
  19. javascript中window.location.search方法简介
  20. Netty组件介绍(转)

热门文章

  1. Repeater和Gridview前台显示行号的方法
  2. Stimulsoft Reports报表工具
  3. PHP发起get post put delete请求
  4. CCNP第二天 帧中继综合实验
  5. CorelDRAW 文件实用工具 CDRTools 2
  6. Linux学习系列之Linux入门(一)linux安装与入门
  7. 奖学金评分系统(系统分析与设计版与Delphi实现代码)
  8. [转]JAVA布局模式:GridBagConstraints终极技巧
  9. PHP数组的操作
  10. C# Windows - TabControl