Android开机自动运行
1. 概述
要开机自动运行某些代码,可以通过boot completed广播事件来实现。具体包括如下几个步骤:
1. 定义一个receiver,监听boot completed事件,从而执行某些代码;
2. 在manifest文件中声明这个receiver,并声明boot completed事件;
3. 增加权限
2. 示例代码
2.1 Java代码
package com.example.testrunner;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;public class MyReceiver extends BroadcastReceiver {private static final String TAG = "TestRunner";@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();Log.d(TAG, "onReceive()");if (action != null && action.equals(Intent.ACTION_BOOT_COMPLETED)) {Log.d(TAG, "boot completed event is received.");doSomething(context);}}private void doSomething(Context context) {Log.d(TAG, "doSomething(), e.g. sava a string information.");String filename = "backup.dat";String savedData = "0123456789";FileOutputStream outStream = null;try {outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);} catch (FileNotFoundException e) {Log.e(TAG, "openFileOutput() error", e);return;}try {outStream.write(savedData.getBytes());} catch (IOException e) {Log.e(TAG, "write() error", e);}try {outStream.close();} catch (IOException e) {Log.e(TAG, "close() error", e);}}}
2.2 manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.testrunner"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="14"android:targetSdkVersion="19" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><!-- <activityandroid:name="com.example.testrunner.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity>--><receiverandroid:name=".MyReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver></application></manifest>
Android开机自动运行相关推荐
- Android开机自动运行APP——BroadcastReceiver
前言: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以"Don't call me, I' ...
- android开机自动运行程序
背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED. ...
- Android 开机自动运行和添加删除桌面快捷方式
<一>开机自启动 当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_C ...
- Android实现开机自动运行程序
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以"Don't call me, I'll c ...
- /etc/rc.local 与 /etc/init.d Linux 开机自动运行程序
1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里 --------引用---------------------- 在完成 run level 3 ...
- Linux实现开机自动运行普通用户脚本
既然要实现开机自动运行脚本,那么就与Linux开机自启服务的原理有关:Linux开机的时候,会有一步读取/etc/inittab配置文件,选定系统运行级别,然后进行系统初始化,执行/etc/rc.d/ ...
- 详解Windows开机自动运行
详解Windows开机自动运行 一.经典的启动--"启动"文件夹 单击"开始→程序",你会发现一个"启动"菜单,这就是最经典的Windows ...
- java开机自动运行,怎么用java实现程序开机自动运行
当前位置:我的异常网» J2SE » 怎么用java实现程序开机自动运行 怎么用java实现程序开机自动运行 www.myexceptions.net 网友分享于:2013-02-17 浏览:69 ...
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)...
#region//开机自动运行 private void CB_Auto_CheckedChanged(object sender, EventArgs e) {//C ...
- nvidia jetson agx Xavier can 开机自动运行脚本
测试can的时候一遍遍手动加载特别麻烦,直接写脚本自动加载.记录,备忘. 一.接线图 二.安装依赖 sudo apt install busybox sudo apt install can-util ...
最新文章
- block之--- 基本使用
- string 换行符_Java 11 已发布,String 还能这样玩!
- P1282 多米诺骨牌 (差值DP+背包)
- 关于oracle with as用法
- MVC系列学习(十一)-客户端的验证
- [转载] 递归函数python基例_python递归函数详解 python 递归函数使用装饰器
- 微信小程序服务器端语言,微信小程序后端用什么语言好
- mysql 添加 删除索引(index) alter table 修改字段 修改列
- python生成一个四位数字的随机数
- Function ‘MseLossBackward0‘ returned nan values in its 0th output.
- 回车符号和换行符号产生背景
- ROS激光雷达导航调试记录
- pyecharts各省人口GDP可视化分析
- 如何用html制作快速邮箱发到手机qq,简单两步使用node发送qq邮件的方法
- 冯氏结构、哈佛结构、超级哈佛结构之间的异同
- 系统分析与设计--BCE详细设计和框架映射
- 无锡设计培训——室内设计方案图纸包括哪些?
- 可将医学计算机应用课程,医学专业大学生计算机理论研究
- Unity-关于一个3D贪吃蛇的走路算法
- ddr2代内存最大升级到多少_升级内存需要看哪些参数
热门文章
- Openbravo怎么给工具栏添加一个按钮
- 更好用的冒泡排序、双向冒泡排序,拓展荷兰国旗问题
- java 中counter什么意思_方便适用的计数器Counter
- 实验12 网络资源共享
- vue结合百度地图api实现地址检索、路线规划等功能
- C++程序练习-1008:Maya Calendar-玛雅日历
- Java实现搭积木_Java搭积木游戏
- Buffer() is deprecated due to security and usability issues.webpack 报错
- RabbitMQ无法连接
- 2020高考数学:常用知识点公式第四章(文科)