Android 接收系统广播,开机启动Service,SMS,Battery

2024-09-07 05:08

本文主要是介绍Android 接收系统广播,开机启动Service,SMS,Battery,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文内容摘自《疯狂Android讲义 第3版》李刚 著
这里写图片描述

自动开启的Service:

package shortcut.song.com.myapplication;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;import java.util.Timer;
import java.util.TimerTask;/*** Created by Administrator on 2017/8/17 0017.*/public class LauncherService extends Service {Timer mTimer = null;TimerTask mTimerTask = null;public LauncherBind myLauncherBind = new LauncherBind();public class LauncherBind extends Binder {}@Nullable@Overridepublic IBinder onBind(Intent intent) {return myLauncherBind;}@Overridepublic boolean onUnbind(Intent intent) {return super.onUnbind(intent);}@Overridepublic void onCreate() {super.onCreate();mTimer = new Timer();mTimerTask = new TimerTask() {@Overridepublic void run() {System.out.println("======LauncherService:"+ System.currentTimeMillis());}};mTimer.schedule(mTimerTask, 500, 1500);}
}
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;/*** Created by Administrator on 2017/8/17 0017.*/public class LauncherBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Intent tIntent = new Intent(context, LauncherService.class);// 启动指定的Servicecontext.startService(tIntent);}
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/><receiver android:name=".LauncherBroadcastReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"/></intent-filter>
</receiver>

SMS:

    <uses-permission android:name="android.permission.READ_SMS"/><receiver android:name=".SmsBroadcastReceiver"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;/*** Created by Administrator on 2017/8/17 0017.*/public class SmsBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if ( intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {// 取消广播续继传播放abortBroadcast();StringBuilder sb = new StringBuilder();Bundle bundle = intent.getExtras();if (bundle != null) {Object[] pdus = (Object[])   bundle.get("pdus");SmsMessage[] messages = new SmsMessage[pdus.length];for (int i=0; i < pdus.length ; i++)  {messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);}for (SmsMessage message : messages) {sb.append("SMS src:");sb.append(message.getDisplayOriginatingAddress());sb.append(message.getDisplayMessageBody());}}Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show();}}
}

Battery:

    <uses-permission android:name="android.permission.BATTERY_STATS"/><receiver android:name=".BatteryReceiver"><intent-filter><action android:name="android.intent.action.BATTERY_CHANGED"/><action android:name="android.intent.action.BATTERY_LOW"/></intent-filter></receiver>
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;/*** Created by Administrator on 2017/8/17 0017.*/public class BatteryReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();int current = bundle.getInt("level");int total = bundle.getInt("scale");if (current * 1.0 / total < 0.15 ){Toast.makeText(context, "Battery Low!!!", Toast.LENGTH_SHORT).show();}}
}

这篇关于Android 接收系统广播,开机启动Service,SMS,Battery的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144145

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期