晚上敲代码的时候,在一个Activity中给按钮添加点击事件,希望实现开启/关闭服务的功能。但是服务死活开启不了。反反复复检查了很多遍,Activity中的代码肯定是没有问题的。

settingBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(getApplicationContext(), AntiService.class);

if (ServiceTool.getServiceState(getApplicationContext(), "com.example.dio.service.AntiService")) {//如果已经打开了,就将按钮关闭,并且关闭服务

stopService(intent);

settingBtn.setSelected(false);

} else {//如果没有打开,将按钮打开,并且开启服务

startService(intent);

settingBtn.setSelected(true);

}

}

});

最后才发现原来是在Manifest.xml文件中给Service下的enable和exported两个标签的设置问题。

下面是官方文档对这两个标签的解释:

android:enabled Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".

The element has its own enabled attribute that applies to all application components, including broadcast receivers. The and attributes must both be "true" for the broadcast receiver to be enabled. If either is "false", it is disabled; it cannot be instantiated.

android:exported Whether or not components of other applications can invoke the service or interact with it — "true" if they can, and "false" if not. When the value is "false", only components of the same application or applications with the same user ID can start the service or bind to it.

The default value depends on whether the service contains intent filters. The absence of any filters means that it can be invoked only by specifying its exact class name. This implies that the service is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the service is intended for external use, so the default value is "true".

This attribute is not the only way to limit the exposure of a service to other applications. You can also use a permission to limit the external entities that can interact with the service (see the permission attribute).

简单翻译一下

android:enabled 定义服务能否被系统实例化的标签,true表示可以实例化,false不能实例化,默认为true。

标签也有enabled标签,这个标签适用于application下所有组件。只有当和下enabled标签的属性都为true的时候,才可以将广播接受者启动(enabled),否则广播接受者不能开启(disabled),不能被实例化。

android:exported 定义服务能否被外部应用的组件调用或者交互,true表示可以,false表示不能。如果设置为false,服务只能接收本应用的组件或者是具有相同用户ID的应用所发出的所开启或绑定。

exported标签的默认属性根据该广播接受者是否有intent filter决定。如果没有定义任何intent filter,那么该服务只能由指定准确的类名(完整类名)的intent对象所调用,这就意味着服务只能在应用内部使用(因为其他应用通常不能获取到完整类名)。这种情况下,默认值就是false。而当服务中存在至少一个intent filter时,就意味着其可以在外部被调用,所以默认值就是true。

这个标签的属性并不是限制服务对外暴露的唯一方式,通过权限(permission)也可以实现。

除了service,receiver也有这两个标签,分别对应的是能否被实例化和能否接受外部消息。

因为之前一开始用eclipse的时候,添加服务并不会提示这两个标签,应该都是默认的。后来用Android Studio,当在应用中创建BroadReceiver的时候,会默认的在Manifest.xml中注册并且添加这两个标签。之前因为没有关注过着两个标签,所以一直默认的。这次不知道什么时候都设置成了false,所以才会无法创建这个服务。

还是平时文档看的不够仔细,学习Android还有很长的路要走。

android:enabled=true service,Manifest中android:enabled和android:exported标签相关推荐

  1. android+jni+构造函数,在JNI中调用构造函数失败Android

    我想从我的JNI Android代码调用构造函数;但不知何故,它失败,以下例外.. 我相信我失去了一些非常小的东西;但我无法弄清楚......任何人都可以请指出?在JNI中调用构造函数失败Androi ...

  2. android 客户端使用service处理用户名和密码验证,android透过webservice验证用户

    当前位置:我的异常网» Web前端 » android透过webservice验证用户 android透过webservice验证用户 www.myexceptions.net  网友分享于:2015 ...

  3. android studio 代码覆盖率,AndroidStudio中使用Jacoco统计Android应用程序代码覆盖率

    AndroidStudio中使用Jacoco统计Android应用程序代码覆盖率 AndroidStudio中使用Jacoco统计Android应用程序代码覆盖率 最近在做针对Android应用程序代 ...

  4. Android使用本地Service实现后台播放音乐

    配置文件 <service android:name=".MyService"></service> 布局 <Buttonandroid:id=&qu ...

  5. Android群英传》读书笔记 (3) 第六章 Android绘图机制与处理技巧 + 第七章 Android动画机制与使用技巧...

    第六章 Android绘图机制与处理技巧 1.屏幕尺寸信息 屏幕大小:屏幕对角线长度,单位"寸": 分辨率:手机屏幕像素点个数,例如720x1280分辨率: PPI(Pixels ...

  6. android 自动替换资源文件,简单高效的实现Android App全局字体替换

    Android O推出了一项新的功能「Fonts in XML」,借助这项功能,我们能够像使用其他资源文件一样使用字体,比较方便地实现App全局字体的替换. 为了能够在API 14或者以上的设备上使用 ...

  7. 【Android 安全】DEX 加密 ( Application 替换 | 分析 Service 组件中调用 getApplication() 获取的 Application 是否替换成功 )

    文章目录 一. Service 中的 getApplication() 方法分析 二. ActivityThread 中的 H 处理 CREATE_SERVICE 消息 三. ActivityThre ...

  8. Android学习笔记(八)——在Manifest中设置ActionBar

    如果想要去掉默认的导航栏 在Manifest的application中添加属性 android:theme="@style/Theme.Design.NoActionBar" 可将 ...

  9. Android如何配置init.rc中的开机启动进程(service)【转】

    本文转载自:http://blog.csdn.net/qq_28899635/article/details/56289063 开篇:为什么写这篇文章 先说下我自己的情况,我是个普通的大四学生,之前在 ...

  10. android如何保证service不被杀死

    Android开发之如何保证Service不被杀掉(broadcast+system/app) http://blog.csdn.net/mad1989/article/details/2249251 ...

最新文章

  1. 特斯拉无人驾驶却在高速路驰骋,四名乘客喝酒唱歌开party,网友:12分应该扣给谁?...
  2. 3.4 usermod命令 3.5 用户密码管理 3.6 mkpasswd命令
  3. minist读取一张图片
  4. linux编辑模式复制快捷键,Linux vim删除、复制、粘贴快捷键
  5. 27.泛型generics.rs
  6. 如何将Pcm格式的音频文件转换成Wave格式的文件
  7. Oracl数据库中大数据的备份-1
  8. mysql c测试程序_Linux平台下从零开始写一个C语言访问MySQL的测试程序
  9. Spring Boot学习总结(24)——Spring Boot 2.5 新特性一览
  10. C#委托和事件的应用Observer模式实例
  11. linux下编译复数类型引发的错误:expected unqualified-id before '(' token
  12. Bilibili宋红康老师MySQL高级篇笔记-架构篇(有完整的md格式笔记,迟点整理好会挂链接)
  13. 一文详解神经网络 BP 算法原理及 Python 实现
  14. c++项目开发——吃豆子游戏
  15. 证券知识库:股票——基础概念
  16. Multiple dex files define Lrx/android/plugins/RxAndroidPlugins;
  17. 图像滤镜艺术---乐高像素拼图特效
  18. Tableau字符串拼接
  19. MongoDB全面总结
  20. 很多我们以为一辈子都不会忘记的事情,就在我们念念不忘的日子里,被我们遗忘了。

热门文章

  1. 网站被黑客攻击怎么办?
  2. 仿新浪微博布局学习——妙用TabHost
  3. SSD-tensorflow-1 demo
  4. 机器学习可解释性(二) 之 可解释模型
  5. 怎么用优动漫PAINT做出色彩的朦胧感?
  6. python有理数_Python3标准库:fractions有理数
  7. CMake入门使用(一)安装及HelloWorld的构建
  8. 简易的JavaScript文字跟随鼠标移动特效
  9. K-median 算法
  10. python之Srcapy框架浅谈