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 ...
最新文章
- mysql多表分页查询语句_Mysql多表分页查询
- Nature Methods:宏基因组物种组成分析工具MetaPhlAn2
- javascript 判断字符串中是否包含某个字符串
- 请简述什么是spring的ioc和di_绿茶用什么茶叶罐储存?有6种茶叶罐适合
- docker mysql开发环境_Docker 构建PHP+Apache+MySQL 开发环境
- 一文理解微服务架构下的系统可用性如何保证?
- C#——《C#语言程序设计》实验报告——Windows桌面编程
- java 语法 冒号_java中生僻的冒号跳转语法
- 某公司有一台打印、复印一体机,需要将购买成本分年均摊到隔年的费用中。请编写一个程序,根据用户输入的购买金额和预计使用年限计算每年的分摊费用。要求对输入异常进行适当的处理。
- 多线程并发之原子性(六)
- php80端口改成8080,jetty,_JETTY 8080端口改为80端口无法访问,jetty - phpStudy
- 解决方案:IndentationError: unindent does not match any outer indentation level
- 405 not allowed什么意思_clock是什么意思
- iOS开发——APP回退到历史版本
- 全概率公式和贝叶斯公式(转载)
- 数据库基础知识(1)--数据库php连接
- 淘汰我们的不是这个时代,而是原地踏步的自己
- 【易康eCognition】面向对象的图像分类学习
- php只取时间的下士_闲来无聊,用python抓取天气信息,简单就是美啊
- 同学录退出了历史舞台了吗?