Android 创建单独的服务运行在后台(无界面)
2017-02-22 11:35
288 查看
今天项目有个需求是,开启一个服务单独运行在后台,而且还不能有界面,在度娘搜索了一圈也没发现可以完美解决的方法,然后自己尝试解决的方法,开始的思路是,把界面干掉,也就是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>
运行程序,服务开启了,界面也不显示,完美解决了后台启动服务的进程。
因为我把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 创建单独的服务运行在后台(无界面)
- android 创建后台运行的服务程序
- android 后台运行服务之创建后台服务篇
- android 功能代码----(AlarmManager保证后台服务长时间在后台运行)
- android后台运行服务,加验证
- android中判断服务是否在后台运行的工具类
- 通过短信窃听器来讲解内容提供者,内容观察者,以及无界面后台运行服务,开机启动和杀死服务后重新启动
- android中获取手机后台运行服务的方法
- android 程序强制停止后服务后台启动运行 ---- 不同程序间相助 使用第三方服务
- 如何检查Android后台服务线程(Service类)是否正在运行
- 通过短信讲解内容提供者,内容观察者,以及无界面后台运行服务,开机启动和杀死服务后重新启动
- Android官方开发文档Training系列课程中文版:后台服务之IntentService的创建
- android 后台运行服务之发送工作请求给后台服务篇
- Android 系列 4.6在显示其他应用程序的同时运行后台服务
- 如何检查Android后台服务线程(Service类)是否正在运行
- 如何检查Android后台服务线程(Service类)是否正在运行
- 如何检查后台服务(Android的Service类)是否正在运行?
- 通过短信***来讲解内容提供者,内容观察者,以及无界面后台运行服务,开机启动和杀死服务后重新启动
- Android创建Service后台常驻服务并使用Broadcast通信
- android 后台服务长时间运行---解决方案