Android 电量监控、关机、重启功能的实现
本文主要是介绍Android电量监控、关机、重启功能的实现,需要具备的条件是手机需要root过,才能实现相关操作。
1.MainActivity.java
import java.io.IOException;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity {private String TAG="MainActivity";private Button startBtn;private Button stopBtn;private Button rebootBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(MainActivity.this,BatteryCheckService.class);startService(intent);//开启服务Log.d(TAG,"开启服务");//初始化控件rebootBtn= (Button) findViewById(R.id.reboot);startBtn =(Button)findViewById(R.id.start);stopBtn =(Button)findViewById(R.id.stop);//开始启动电量监控startBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this,BatteryCheckService.class);startService(intent);}});//停止电量监控stopBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this,BatteryCheckService.class);stopService(intent);}});//重启机器rebootBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString cmd = "su -c reboot";try {Runtime.getRuntime().exec(cmd);} catch (IOException e) {// TODO Auto-generated catch block
/*new AlertDialog.Builder(this).setTitle("Error").setMessage(e.getMessage()).setPositiveButton("OK", null).show();*/}}});}}
2.BatteryCheckService.java
import java.io.DataOutputStream;
import java.io.IOException;import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast;public class BatteryCheckService extends Service {/* 变量声明 */private int intLevel;private int intScale; /* create BroadcastReceiver */private BroadcastReceiver mBatInfoReceiver=new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); /* 如果捕捉到的action是ACTION_BATTERY_CHANGED,* 就执行onBatteryInfoReceiver() */if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { intLevel = intent.getIntExtra("level", 0); intScale = intent.getIntExtra("scale", 100); onBatteryInfoReceiver(intLevel,intScale);} } };@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic int onStartCommand(Intent intent,int flags,int startId){// setContentView(R.layout.activity_main);/* 注册几个系统 BroadcastReceiver,作为访问电池计量之用 */ registerReceiver (mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED) );return super.onStartCommand(intent,flags,startId);}/* 拦截到ACTION_BATTERY_CHANGED时要执行的method */public void onBatteryInfoReceiver(int intLevel, int intScale) {// 如果当前电量小于总电量的10%if (intLevel* 1.0 /intScale < 0.10){Toast.makeText(getApplicationContext(), "当前手机电量剩余"+String.valueOf(intLevel * 100 / intScale) + "%"+",电量过低,请尽快充电!", Toast.LENGTH_LONG).show();shutdown() ;}else{Toast.makeText(getApplicationContext(), "当前手机电量剩余"+String.valueOf(intLevel * 100 / intScale) + "%"+"电量正常,可以正常使用!", Toast.LENGTH_LONG).show();}}//关机private void shutdown() { try { Process process = Runtime.getRuntime().exec("su"); DataOutputStream out = new DataOutputStream( process.getOutputStream()); out.writeBytes("reboot -p\n"); out.writeBytes("exit\n"); out.flush(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDestroy() {//重写onDestroy方法 this.unregisterReceiver(mBatInfoReceiver);//取消注册的CommandReceiver super.onDestroy(); }
}
3.布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="60dip"android:textStyle="bold"android:textSize="24dip"android:text="电量监控" android:layout_gravity="center"/><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dip"android:layout_gravity="center"><Buttonandroid:id="@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="开始"/><Buttonandroid:id="@+id/stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="停止"/><Buttonandroid:id="@+id/reboot"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="重启"/></LinearLayout></LinearLayout>
4.配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.batterycheckserver"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="21" /><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=".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> <service android:name=".BatteryCheckService"></service><!-- <receiver android:name=".BootReceiver"> <intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"/><category android:name="android.intent.category.LAUNCHER" /> </intent-filter></receiver> --></application></manifest>
Android 电量监控、关机、重启功能的实现相关推荐
- android电池电量监控,android电量监控 Battery Widget Reborn
android电量监控 Battery Widget Reborn Pro 你一款管理安卓手机电池的使用软件,其中包括对电池的基本使用信息.使用寿命.电容等方面对电池进行分析管理,提高电池的使用率,安 ...
- android studio 模拟器关机重启操作
测试广播功能,接收开机广播练习: 创建receiver,Android Manifest.xml文件注册两个地方 1.权限 <uses-permission android:name=" ...
- Android电量监控与优化
仅适用于android5.0以上系统. 1.使用Battery Historian对耗电记录文件进行分析 这篇文章里有讲如何通过adb命令获取某一段时间内电量消耗记录文件,然后用Battery His ...
- Android电量监控
- linux 监控进程重启,linux进程监控与自动重启的简单实现方法
linux进程监控与自动重启的简单实现方法 目的: linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能. 实现原理: 由定时任务crontab调用 ...
- Android实现系统关机和重启
Android实现系统关机和重启 一.将APP提升至系统权限 在AndroidMenifest.xml中添加如下代码: android:sharedUserId="android.uid.s ...
- 为Android系统定制重启功能
按计划每周更新一篇技术博文,第二篇:<为Android系统定制重启功能> 一.Android系统重启的实现方式 1.广播方式 之前的博文介绍过这种方式<使用广播实现的Android关 ...
- 【Android界面实现】AppWidght全面学习之电量监控小部件的实现详解
前几天翻看之前下载的各种资料,无意中发现了一款AppWidght应用的源代码,想起之前一直想研究这块,却一直没机会,于是花费了两天时间,把这款桌面电量监控小插件的实现研究了一下,收获颇丰,特此把学到的 ...
- Android手机开关机、重启时间优化
本篇文章主要介绍 Android 开发中开关机 重启时间 部分知识点,通过阅读本篇文章,您将收获以下内容: zygote,预加载class.resources 开机动画进程 bootanimation ...
最新文章
- 他本科来自普通二本,读博后3年迎来2篇Nature,创造校史!
- 报名 | 高级管理人员AI大数据能力研修班
- 人工智能助力全国大学生智能车竞赛
- elasticsearch聚合操作——本质就是针对搜索后的结果使用桶bucket(允许嵌套)进行group by,统计下分组结果,包括min/max/avg...
- MFC 单文档的全局变量
- android TextView 的垂直滚动
- MYSQL8 度分秒(DMS)转度(DDD)函数编写实战
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- 苹果零售业逐步恢复:部分已允许顾客店内试用AirPods
- 1小时搞懂设计模式之策略模式
- svn-the working copy is locked due to a previous
- win11小键盘怎么设置 Windows11小键盘的设置方法
- Cocos2d-x 3.0修改Android平台帧率fps - 解决游戏运行手机发热发烫问题
- Content-Disposition的使用和注意事项
- TV版应用包名 TV常用apk包名 (当贝市场下载)
- C#窗体 绘画一棵树
- 需要administrator权限才能删除文件的处理方法
- C# VS2017中Windows窗体更改图标
- 饱受争议的区块链手机兴衰史
- iphone充电图_哪些iPhone具有无线充电功能?
热门文章
- 计算机网络安全清华大学出版答案,计算机网络安全清华大学出版答案
- 中国已在税务系统中使用区块链技术
- [Facebook] 如何建立facebook App 且取得FB ID
- 多测师肖sir_高级金牌讲师_项目介绍之保险相关专业知识
- 引脚间距是0.4mmBGA(WLCSP)封装的芯片怎么打孔走线
- nginx 一个站点支持多端口配置
- python 对象转换为json,Python对象转换为json的方法步骤
- macOS开发 -Packages的使用
- Android自定义View--自己撸一个柱状图也没那么难
- HDU - I Hate It(线段树)