Android开发之通过Intent启动其他App的Service
2015-07-11 17:50
477 查看
在Android5.0以前可以通过隐式Intent方式启动其他App的Service,就跟Activity启动隐式Intent一样的。
但是在5.0以后,只能使用显示的Intent方式启动了。
启动其他App的Service,需要用到Intent的setComponent()方法。该方法需要传入ComponentName component 这个参数。
参数的解释:component, The name of the application component to handle the intent, or null to let the system find one for you.
代码:
这样就能启动其他App的Service。但是需要先设置其他App的Service的
否则会报错, java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.startservicefromanotherapp/.AppService } without permission not exported from uid 10075
提示没有权限。
但是在5.0以后,只能使用显示的Intent方式启动了。
启动其他App的Service,需要用到Intent的setComponent()方法。该方法需要传入ComponentName component 这个参数。
参数的解释:component, The name of the application component to handle the intent, or null to let the system find one for you.
代码:
Intent serviceIntent; private Button btnstartService; private Button btnstopService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent=new Intent(); serviceIntent.setComponent(new ComponentName("com.example.startservicefromanotherapp", "com.example.startservicefromanotherapp.AppService")); btnstartService=(Button) findViewById(R.id.btnStartService); btnstopService=(Button) findViewById(R.id.btnStopService); btnstartService.setOnClickListener(this); btnstopService.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btnStartService: startService(serviceIntent); break; case R.id.btnStopService: stopService(serviceIntent); break; } }
这样就能启动其他App的Service。但是需要先设置其他App的Service的
android:exported="true"
否则会报错, java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.startservicefromanotherapp/.AppService } without permission not exported from uid 10075
提示没有权限。
相关文章推荐
- Android 计时器使用
- APP store 官方统计工具的常见的Q&A
- Cocos Studio工程分析
- iOS: 由第三方SDK引起 duplicate symbol 的思考
- 使用Cocos Studio创建一个简单的工程
- iOS项目的完整重命名方法图文教程
- android生成bks keystore 记录 & 使用portecle把JKS密钥库转成BKS类型
- iOS项目中常用的第三方开源库
- Android总结之Animation
- iOS中对时间的处理(三)——各时区时间相互转化
- 微信公众号通过链接实现关注(盗用广告主流量主)
- ios 设置相机照片库中文
- 25个增强iOS应用程序性能的提示和技巧
- 我的Android进阶之旅------>Android实现音乐示波器、均衡器、重低音和音场功能
- 解决android下Couldn't resolve resource @string/xx 问题
- [Android Pro] proguard.cfg 配置文件
- android相对布局的各种属性
- Cocos2d-x制作跳棋第三步:棋子动作实现及AI算法思想
- Android笔记-1
- [转载]Android开发必备的21个免费资源和工具