Android开机自启动服务的写法
首先AndroidManifest.xml中<application>结点中要加入一段广播接收器接收name为"android.intent.action.BOOT_COMPLETED"的开机广播,其中"com.example.noemhost.BootBroadcastReceiver"就是让系统接受到广播之后通过反射调用的广播接收类:
<!--随着安卓系统启动而启动-->
<receiver android:name="com.example.noemhost.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
广播接收类的写法:
接收到启动完成的广播后,你可以利用回调给你的context上下文通过startActivity启动一个Acitivity,或者利用startService启动一个服务,这样开机时便会运行你的后台服务但是不实际显示任务于任务栏中了。
package com.example.noemhost;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Intent welcomeIntent = new Intent(context, DetectService.class);
// context.startActivity(welcomeIntent);
context.startService(welcomeIntent);
}
}
}
一个示例服务:
package com.example.noemhost;
import android.R.interpolator;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/** 指纹搜索服务 **/
public class DetectService extends Service {
private boolean canRun = true;
@Override
public IBinder onBind(Intent intent) {
return null;
}
private boolean firstRun = true;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "WTF", Toast.LENGTH_SHORT).show();
}
}
完整的AndroidManifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.noemhost"
android:versionCode="1"
android:versionName="1.3"
>
<!--
android:process="system"
android:sharedUserId="android.uid.system"
-->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:logo="@drawable/ic_launcher">
<service android:name="com.example.noemhost.DetectService" >
</service>
<!--android:theme="@android:style/Theme.NoDisplay" -->
<!-- <category android:name="android.intent.category.DEFAULT" /> -->
<activity
android:name="com.example.noemhost.MainActivityVer2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
</activity>
<!--随着安卓系统启动而启动-->
<receiver android:name="com.example.noemhost.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
————————————————
版权声明:本文为CSDN博主「cjzcjl」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cjzjolly/article/details/81333295
Android开机自启动服务的写法相关推荐
- android自动运行服务,Android开机自启动服务的实现方法
public class BootReceiver extends BroadcastReceiver { private PendingIntent mAlarmSender; @Override ...
- Android开机自启动C程序调试
Android开机自启动C程序调试 本次记录是关于如何在rk3566的Android11版本下将led时钟显示添加成开机自启动的C程序. 首先,当然是在sdk中会被执行到的.rc文件中将我们所需要执行 ...
- centos7 systemctl配置开机自启动服务
centos7 systemctl配置开机自启动服务 centos7使用systemctl替代原来/etc/init.d,按官方的说法是提高系统服务的运行效率.服务配置更加简单易用,对于一些自定义的服 ...
- 将自己写的脚本添加至开机自启动服务和chkconfig的原理
将自己写的脚本添加至开机自启动服务和chkconfig的原理 创建脚本测试 [root@anuo ~]# vim anuo.sh --创建个脚本 # !/bin/bash # chkconfig: 3 ...
- linux 设置开机自启动服务命令,linux开机自启动服务优化设置命令
1.设置成英文字符,避免出现乱码 [root@xuegod62 ~]# LANG=en 2.两种配置linux开机自启动服务命令: 1) [root@xuegod62 ~]# ntsysv 2) [r ...
- linux实现开机自启动服务/脚本
linux实现开机自启动服务或者开机自运行用户自定义脚本有三种方式 方式一: 执行 systemctl enable 服务名 命令,将对应服务的.service文件软链接到 /etc/systemd/ ...
- Android 开机自启动
首先实现开机自启动: 第一步创建一个广播接收者,如MyBootBroadcastReceiver.java View Codepackage com.example;import android.co ...
- [Android]开机自启动脚本和selinux权限配置
概述 在前段时间的工作中,需要开发一个开机自动启动的脚本,现把开发过程记录一下 主要框架 编写一个可以开机自动启动的脚本,方法就是通过rc文件,在boot_complete=1时,去启动这个服务,那么 ...
- android 开机启动服务。
今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.in ...
- Centos 下添加开机自启动服务和脚本
最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...
最新文章
- centos mysql安装_mysql yum源安装
- 【每周CV论文推荐】 初学深度学习图像分割必须要读的文章
- Druid 数据源连接池配置
- PHPUnit-附录 A. 断言 (assert)
- Java 虚拟机对锁优化所做的努力
- VBA读取固定文件夹中txt内容
- LintCode—删除链表中倒数第n个节点(174)
- 实验三mysql查询_实验三 数据库的查询实验
- 系统安装部署系列教程(六):封装系统
- Mac本地账号开机密码破解
- python通过cookie跳过登录
- Python学习:简单的python “hello,world”程序
- 生产者与消费者的实现
- kpu 处理器_KPU · MaixPy 文档
- 程序员的目标不是赚钱而是向牛人看齐(转)
- SEO搜索引擎优化教程-传智-专题视频课程
- 每天学点统计学——平均
- (C语言)练习8——老--质价比
- 在Ubuntu上搭建Maemo 5及Qt 4开发环境
- matplotlib画直线
热门文章
- nodeJS版本升级
- 一代人终将老去,但总有人正年轻
- 怎么用matlab画国旗,Matlab考试攻略_国旗题_1.如何用Matlab画法国国旗?
- 玛雅日历转化(Maya calendar,POJ1008, UVA300)
- 考研英语九附双语阅读:英国品牌美国遇冷 美国人不待见英国货?
- 实例化Servlet类异常404、500错误-解决方法
- [车]上海外地“苏牌”竟要万元
- phalapi init.php,PhalApi:[1.12] 参数规则:接口参数规则配置
- python实现飞机大战源代码+素材+项目分析
- 2021年国外网络安全博客和网站排名