Android 创建单独的服务运行在后台(无界面)
转自:https://blog.csdn.net/a704225995/article/details/56481934
今天项目有个需求是,开启一个服务单独运行在后台,而且还不能有界面,在度娘搜索了一圈也没发现可以完美解决的方法,然后自己尝试解决的方法,开始的思路是,把界面干掉,也就是activity,然后将开启Service的操作放在Application中,结果运行程序,在控制台报错了。
因为我把AndroidManifest.xml中的主Activity的配置给干掉了,而程序找不到应用的入口,所有就无法打开应用,这种方法行不通。
然后我就想,把Activity保留,但是我不给它 setContentView(......);也就是不给他设置布局文件,
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- System.out.println("MainActivity OnCreate()....");
- System.out.println("准备开启服务");
- Intent intent = new Intent(MainActivity.this,TestService.class);
- startService(intent);
- }
- }
运行程序,程序打开了,服务也运行了,但是有个问题就是,界面也出来了,为什么呢?
原因是在AndroidManifest.xml中Application节点中这个这行代码android:theme="@style/AppTheme",既然是主题的问题导致界面的出现,那么是想android是否提供了不显示界面的主题?查找后问题终于解决了,解决方法:在清单文件中,主activity的配置中添加这行代码
android:theme="@android:style/Theme.NoDisplay"
代码:
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name"
- android:theme="@android:style/Theme.NoDisplay"
- >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:name="com.example.backgroundservice.TestService" >
- </service>
- </application>
我们还可以Ctrl+左键点进去看看这个主题是怎么写的:
- <!-- Default theme for activities that don't actually display a UI; that
- is, they finish themselves before being resumed. -->
- <style name="Theme.NoDisplay">
- <item name="android:windowBackground">@null</item>
- <item name="android:windowContentOverlay">@null</item>
- <item name="android:windowIsTranslucent">true</item>
- <item name="android:windowAnimationStyle">@null</item>
- <item name="android:windowDisablePreview">true</item>
- <item name="android:windowNoDisplay">true</item>
- </style>
运行程序,服务开启了,界面也不显示,完美解决了后台启动服务的进程。
Android 创建单独的服务运行在后台(无界面)的更多相关文章
- linux服务器上部署springboot项目,并让他持续运行到后台
我们知道在运行springboot 项目只需要java -jar + 项目的war包(jar包)名. 但是只要终端已停止那么服务就会被关闭,也就无法访问到我们的项目了.所以我们可以使用守护进程的方式来 ...
- 第三百五十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行、scrapy-splash、splinter
第三百五十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行.scrapy-splash. splinter 1.chrome谷歌浏览器无界面运行 chrome ...
- 三十一 Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行、scrapy-splash、splinter
1.chrome谷歌浏览器无界面运行 chrome谷歌浏览器无界面运行,主要运行在Linux系统,windows系统下不支持 chrome谷歌浏览器无界面运行需要一个模块,pyvirtualdispl ...
- 解决Linux关闭终端(关闭SSH等)后运行的程序或者服务自动停止【后台运行程序】
问题描述:当SSH远程连接到服务器上,然后运行一个服务 ./catalina.sh start,然后把终端开闭(切断SSH连接)之后,发现该服务中断,导致网页无法访问. 解决方法:使用nohup命 ...
- [android] 采用服务执行长期后台的操作
服务:在后台长期运行的没有界面的组件 新建一个类PhoneService类,继承系统的Service类 清单文件中 进行配置 新建一个节点<service>,设置名称android:nam ...
- Android开发-API指南-服务
Service 英文原文:http://developer.android.com/guide/components/services.html 采集(更新)日期:2014-12-23 原博客:htt ...
- Android从启动到程序运行整个过程的整理
1Android是基于Linux的一个操作系统,它可以分为五层,下面是它的层次架构图,可以记一下,因为后面应该会总结到SystemServer这些Application Framework层的东西 A ...
- android 应用程序与服务端交互
http://www.cnblogs.com/freeliver54/archive/2012/06/13/2547765.html 简述了Service的一些基础知识以及Service和Thread ...
- Android开发学习—— Service 服务
Service运行于后台的一个组件,用来运行适合运行在后台的代码,服务是没有前台界面,可以视为没有界面的activity. 服务可以被手动关闭,不会重启,但是如果被自动关闭,内存充足就会重启. sta ...
随机推荐
- wordpress更换主题未能连接到FTP服务器
报错原因:由于你的 WordPress 所在的目录没有写入权限,而wordpress安装主题或者更新时,企图通过ftp帐号进行更新,所以无法完成安装或更新 解决办法: 找到wp-config.php文 ...
- python 全栈开发,Day5(字典,增删改查,其他操作方法)
一.字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.存储大量的数据,是关系型数据,查询数据快. 列表是从头遍历到尾字典使用二分查找 二分查找也称折半查找(Bi ...
- 【转】crontab实用手册
前言 crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那么任务就是常驻程序,这对你的程序要求比 ...
- 步步为营-30-AES加密与解密
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C#之app.config、exe.config和vshost.exe.config作用区别
vshost.exe.config是程序运行时的配置文本 exe.config是程序运行后会复制到vshost.exe.config app.config是在vshost.exe.config和exe ...
- Oracle 11.2.0.4 For Windows 64bit+32bit 数据库
1.Oracle11G 32BIT介质官方链接 适用于Windows 32bit的Oracle Database 11G 第2版U4(11.2.0.4)Oracle11.2.0.4 Windows3 ...
- 配置apache和php mysql的一些问题
关于"Windows不能在本地计算机启动Apache2.......并参考特定服务错误代码1"问题解决 apache的httpd.conf文件配置“LoadModule php5_ ...
- 大数据在教育中的应用 part2笔记
什么是交叉检验(K-fold cross-validation) K层交叉检验就是把原始的数据随机分成K个部分.在这K个部分中,选择一个作为测试数据,剩下的K-1个作为训练数据. 交叉检验的过程实 ...
- win10下Spark的环境搭建
win10下Spark的环境搭建 2018-08-19 18:36:45 一.jdk 1.8.0 安装与配置 二.scala 2.11.8 安装与配置http://www.scala-lang.or ...
- CSS3滚动条美化,CSS3滚动条皮肤
CSS3 -webkit-scrollbar滚动条皮肤美化实现,利用-webkit-scrollbar,-webkit-scrollbar-track,-webkit-scrollbar-thumb这 ...