本文主要是介绍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;
@Override
protected 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() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,BatteryCheckService.class);
startService(intent);
}
});
//停止电量监控
stopBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,BatteryCheckService.class);
stopService(intent);
}
});
//重启机器
rebootBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String 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);
}
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public 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">
<TextView
android: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">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"/>
<Button
android: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-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android: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>