Android开机启动广播

  • 理论概述
  • 核心代码

第一章 理论概述

第01节 基础说明

1、开机启动的过程当中, 定义开机启动广播。2、接收到开机启动广播之后, 可以开启 Service A. 低版本实现 Android8.0 之前的实现B. 高版本实现 Android8.0 之后的实现3、接收到开机启动广播之后, 可以开启 ActivityA. 低版本实现 Android8.0 之前的实现B. 高版本实现 Android8.0 之后的实现

第02节 基础步骤

1. 定义清单文件A. 三种权限(开机广播、后台服务、后台Activity)B. 定义三类(Activity、Service、Receiver)2. 定义广播接收者 ReceiverA. 重写方法 onReceive()  B. 分版本开启  ServiceC. 分版本开启  Activity3. 定义服务 ServiceA. 重写方法 onBind()  B. 重写方法 onCreate()  C. 重写方法 onStartCommand()4. 定义 ActivityA. 重写方法 onCreate()

第03节 效果图

开机启动Activity 高版本效果

开机启动Activity 低版本效果

开机启动Service高版本效果

开机启动Service低版本效果

第二章 核心方法

第01节 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="chc.svip"><!--  设置权限  --><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><!--  后台服务的权限  --><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><!--  后台Activity的权限  --><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MyDemo"><!--  定义Activity,接收开机启动的广播当中,跳入到Activity当中 --><activityandroid:name=".MainActivity"android:enabled="true"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- 定义广播接收者,准备用于开机的过程当中,接收开机启动的广播  --><receiverandroid:name=".BootCompleteReceiver"android:enabled="true"android:exported="true"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></receiver><!-- 定义服务Service,接收开机启动的广播当中,跳入到Service当中 --><service android:name=".HelloService" /></application></manifest>

1、 权限定义

2、组件定义

第02节 Activity

import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;public class MainActivity extends AppCompatActivity {private static final String TAG = "hello";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i(TAG, "onCreate: hello ===>MainActivity启动了");}
}

1、成员变量 主要是用于打印的标记 TAG

2、成员方法 主要是 onCreate 重写的方法

第03节 Service

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;import androidx.annotation.Nullable;
/*Android 8.0 系统不允许后台应用创建后台服务,故只能使用Context.startForegroundService()启动服务创建服务后,应用必须在5秒内调用该服务的 startForeground() 显示一条可见通知,声明有服务在挂着,不然系统会停止服务 + ANR 套餐送上。Notification 要加 Channel,系统的要求为什么要在onStart里再次调用startForeground()?答:这一条主要是针对后台保活的服务,如果在服务A运行期间,保活机制又startForegroundService启动了一次服务A,那么这样不会调用服务A的onCreate方法,只会调用onStart方法。如果不在onStart方法里再挂个通知的话,系统会认为你使用了 startForegroundService 却不在 5 秒内给通知,很傻地就停止服务 + ANR 套餐送
*/public class HelloService extends Service {private static final String TAG = "hello";public static final String CHANNEL_ID_STRING = "service_01";private Notification notification;@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {Log.i(TAG, "onCreate: ...服务创建...");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG, "onStartCommand: start....服务启动...");//下面是判断 Android8.0 以后的服务,设置为后台服务的操作NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel mChannel = null;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {mChannel = new NotificationChannel(CHANNEL_ID_STRING, getString(R.string.app_name),NotificationManager.IMPORTANCE_LOW);manager.createNotificationChannel(mChannel);notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();//注意 startForeground(1, notification);中不能为0,不然会出现如下问题//Context.startForegroundService() did not then call Service.startForeground()startForeground(1, notification);}return super.onStartCommand(intent, flags, startId);}
}

1、成员变量

​ A. 用于打印的标记 TAG

​ B. 用于通知的标记 CHANNEL_ID_STRING

​ C. 用于通知的对象 Notification

2、成员方法

​ A. 绑定方法 onBind

​ B. 创建方法 onCreate

​ C. 开启方法 onStartCommand

第04节 Receiver

import static android.content.Context.NOTIFICATION_SERVICE;import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;/**** 开机启动的广播*/
public class BootCompleteReceiver extends BroadcastReceiver {private static final String TAG = "hello";private static final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";@Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context, "Boot Complete 开机启动的广播接收者", Toast.LENGTH_LONG).show();Log.i(TAG, "onReceive: Boot Complete 开机启动的广播接收者");//开机的过程当中,启动 Activity的操作,判断当前启动的动作是开机启动的if (BOOT_ACTION.equals(intent.getAction())) {//开启ActivityopenActivity(context);//开启Service//openService(context);}}/**** 启动Service的方法** @param context*/public void openService(Context context) {Intent newIntent = new Intent(context,HelloService.class);//判断当前编译的版本是否高于等于 Android8.0 或 26 以上的版本if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {context.startForegroundService(newIntent);} else {context.startService(newIntent);}Log.i(TAG, "openService: 启动Service");}/**** 启动Activity的方法** @param context*/public void openActivity(Context context) {//判断当前编译的版本是否高于等于 Android8.0 或 26 以上的版本if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {startActivityVersionHeight(context);}else{startActivityVersionLower(context);}Log.i(TAG, "openActivity: 启动Activity");}/**** 低版本的实现* @param context*/public void startActivityVersionLower(Context context) {Intent myIntent = new Intent(context,MainActivity.class);myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(myIntent);}/**** 高版本的实现* @param context*/public void startActivityVersionHeight(Context context) {Intent intent1 = new Intent(context,MainActivity.class);intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);String channelID = "my_channel_ID";String channelNAME = "my_channel_NAME";int level = NotificationManager.IMPORTANCE_HIGH;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationManager manager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);manager.createNotificationChannel(channel);}NotificationCompat.Builder notification =new NotificationCompat.Builder(context, channelID).setSmallIcon(R.drawable.ic_launcher_background).setContentTitle("Start Activity").setContentText("click me").setPriority(NotificationCompat.PRIORITY_HIGH).setCategory(NotificationCompat.CATEGORY_CALL).setAutoCancel(true).setFullScreenIntent(fullScreenPendingIntent, true);NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);notificationManager.notify(100, notification.build());}
}

1、成员变量

​ A. 打印日志的标记 TAG

​ B. 开机启动的广播 BOOT_ACTION

2、成员方法

​ A. 接收广播的方法 onReceive

​ B. 开启服务的方法 openService

​ C. 开启Activity的方法 openActivity

​ D. 低版本启动 Activity的实现 startActivityVersionLower

​ E. 高版本启动 Activity的实现 startActivityVersionHeight

Android开机启动广播相关推荐

  1. android 开机启动服务。

    今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.in ...

  2. Android开机启动Activity或者Service方法

    这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下 ...

  3. Android开机启动流程

    Android开机启动流程 一.APPS PBL(Application primary boot loader:主引导加载程序) 二.XBL(Extensible boot loader:可扩展引导 ...

  4. Android开机启动流程简析

    Android开机启动流程简析 (一) 文章目录 Android开机启动流程简析 (一) 前言 一.开机启动的流程概述 二.Android的启动过程分析 (1).总体流程 init简述 Zygote简 ...

  5. android 开机启动程序

    做一个android开机就会自动启动的程序,该程序只要启动一次,以后开机就会自动启动,直到删除该程序. android开机事件会发送一个叫做Android.intent.action.BOOT_COM ...

  6. Android 开机启动慢的原因分析

    开机启动花了40多秒,正常开机只需要28秒就能开机起来. 内核的启动我没有去分析,另一个同事分析的.我主要是分析从SystemServer启来到开机动画结束显示解锁界面的这段时间,也就是开机动画的第三 ...

  7. android开机启动service

    为什么80%的码农都做不了架构师?>>>    1.开机启动后系统会发射出一个Standard Broadcast Action,名字叫android.intent.action.B ...

  8. Android 开机启动

    创建一个Receiver,用来监听开机完毕: public class MyReceiver extends BroadcastReceiver {static final String action ...

  9. Android8.0 开机启动脚本,Android开机启动shell脚本(Android 8.0测试OK)

    Android 下做开机启动shell脚本的大致流程如下: 目录 写shell脚本 为脚本写te文件 在init.rc中启动脚本 添加Selinux权限 写shell脚本 比如新建一个init.tes ...

  10. Android开机启动检测和连接wifi检测

    Android启动时,会发出一个系统广播 ACTION_BOOT_COMPLETED,它的字符串常量表示为 "android.intent.action.BOOT_COMPLETED&quo ...

最新文章

  1. oracle 数据库中(创建、解锁、授权、删除)用户
  2. push to origin/master was rejected错误解决方案(IDEA)
  3. 计算机控制闪光灯,摄影技巧 闪灯篇 光圈控制主体 快门控制场景 闪光灯又该如何调整输出功率?...
  4. cocos 时间函数需要什么引用_酱香型白酒,为什么需要长时间储存?
  5. 什么是 ABAP Field Symbol
  6. python qt gui快速编程 pdf_翻译:《用python和Qt进行GUI编程》——介绍
  7. hightcharts 如何修改legend图例的样式
  8. 【国内首套H3C V7交换机实战课程-1】Comware V7使用、维护与管理-王达-专题视频课程...
  9. mega linux教程,MegaRAID工具使用详解
  10. Setycyas的自定义表情油猴插件
  11. C语言经典-报数问题
  12. w10更新后怎么找计算机全民,Win10系统下全民WiFi不能用了怎么办
  13. 淘宝API淘口令真实url
  14. 网络基础——网络层(ip协议详解)
  15. SpringApplicationRunListener
  16. ChatGPT的悄然问世,让原先“吃香”的10种“铁饭碗”快要端不住了:软件技术类、新闻媒体类、法律工作类、市场研究分析师、教师、金融分析类、交易员、平面设计师、会计师、客服人员。
  17. 微信 各种号之间的差异
  18. 程序员:我差点死在了北京黑中介的手里……
  19. 教师计算机校本培训心得,小学教师信息技术应用能力提升工程网络与校本研修心得体会...
  20. Android app开发入门复习一(1-2章)

热门文章

  1. 西门子1200fb284
  2. 《企业IT架构转型之道-阿里巴巴中台战略思想与架构实战》笔记
  3. et中计算机的快捷键,ET制版快捷键
  4. 给出汉字‘你’、‘我’、‘他’在Unicode表中的位置
  5. 百度视频播放器android,百度视频播放器
  6. 《趣谈网络协议》学习笔记
  7. java导出到txt_Java生成TXT文本并下载
  8. 早教机器人刷固件_E-puck2机器人系列教程-固件修复升级
  9. 如何卸载赛门铁克(Symantec)企业防病毒客户端软件SEP(Symantec Endpoint Protection)?
  10. 私塾在线 Java架构师在线课程(148讲教程)