监听开机广播开启service
1. startService方式开启服务,程序返回后,仍在后台运行
2. 多次调用startService会多次开启服务
3. 检测是否已开启了某个服务
List<ActivityManager.RunningServiceInfo> runningServiceInfoList = manager.getRunningServices(200); for (ActivityManager.RunningServiceInfo info : runningServiceInfoList) { //全路径类名 if ((getPackageName()+".ThreadService").equals( info.service.getClassName())){ //implement your code } }
4. 注册系统广播检测是否存在某个服务
Intent.ACTION_TIME_TICK每隔一分钟,系统会发送一次该广播,监听该广播,并通过第(3)步的代码来不间断的判断某个服务是否还存活。注意该广播无须在AndroidManifest.xml中注册
5.开机启动的广播
5.1 在AndroidManifest.xml中添加接收开机广播的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>5.2 继承BroadcastReceiver,该广播通过静态方式注册
public class BootReceiver extends BroadcastReceiver{ @override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { //implement your code } }
5.3 在AndroidManifest.xml中注册该广播
<receiver android:name=".BootCompleteReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
相关推荐
chenjinlong 2020-04-06
xilove0 2020-01-14
magic00 2020-01-10
xilove0 2019-12-09
ThedakeLaugh 2010-08-23
fuzhangandroid 2011-04-07
老菜鸟自习室 2011-08-05
StarkHuang 2014-09-04
mingming 2015-01-13
taiyuanwuyin 2015-03-31
易辰Android 2012-05-08
Alexlee 2018-04-20
OliverLau 2019-06-29
Alexlee 2018-04-20