Android6.0以上实现断电关机

2019-07-14 01:33发布

寻寻觅觅,终于让我找到了一种在安卓6.0以上可以用的关机方法,当然了,前提是手机必须root。因为我要实现的是断电关机,所以首先得检测电源事件。
一、监测电源事件:
1.创建Broad类 package com.slq.shutdownafterdisconnect; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.drm.DrmStore; /** * Created by 15265 on 2017/11/17. */ public class Broad { private BatteryStateListener mBatteryStateListener; private Context mContext; private BroadCast receiver; public Broad(Context context) { mContext = context; receiver = new BroadCast(); } public void register(BatteryStateListener listener) { mBatteryStateListener = listener; if (receiver != null) { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); filter.addAction(Intent.ACTION_BATTERY_LOW); filter.addAction(Intent.ACTION_BATTERY_OKAY); filter.addAction(Intent.ACTION_POWER_CONNECTED); filter.addAction(Intent.ACTION_POWER_DISCONNECTED); mContext.registerReceiver(receiver, filter); } } public void unregister() { if (receiver != null) { mContext.unregisterReceiver(receiver); } } private class BroadCast extends BroadcastReceiver { String action=null; @Override public void onReceive(Context context, Intent intent) { if (intent!=null) { action=intent.getAction(); switch (action) { //接通电源 case Intent.ACTION_POWER_CONNECTED: if (mBatteryStateListener!=null) { mBatteryStateListener.onStatePowerConnected(); } break; //拔出电源 case Intent.ACTION_POWER_DISCONNECTED: if (mBatteryStateListener!=null) { mBatteryStateListener.onStatePowerDisconnected(); } //MyShutDown(); break; //电量低 case Intent.ACTION_BATTERY_LOW: break; //电量发生变化 case Intent.ACTION_BATTERY_CHANGED: break; //电量充满 case Intent.ACTION_BATTERY_OKAY: break; } } } } //监听接口 public interface BatteryStateListener { public void onStatePowerConnected(); public void onStatePowerDisconnected(); } }
二、在MainActivity编写关机方法: private void MyShutDown() { try { Process process=Runtime.getRuntime().exec("reboot -p"); String data=null; BufferedReader errorLine=new BufferedReader(new InputStreamReader(process.getErrorStream())); BufferedReader inputLine=new BufferedReader(new InputStreamReader(process.getInputStream())); String error=null; while ((error=errorLine.readLine())!=null&&!error.equals("null")) { data+=error+" "; } String input=null; while((input= inputLine.readLine())!=null&&!input.equals("null")) { data+=input+" "; } } catch (Exception e) { } }
三、在onCreate中调用 Broad listener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listener=new Broad(this); listener.register(new Broad.BatteryStateListener(){ @Override public void onStatePowerConnected() { Toast.makeText(MainActivity.this,"接通电源",Toast.LENGTH_LONG).show(); } @Override public void onStatePowerDisconnected() { Toast.makeText(MainActivity.this,"断开电源",Toast.LENGTH_LONG).show(); MyShutDown(); } }); }
四、在onDestroy中注销
@Override protected void onDestroy() { if (listener!=null) { listener.unregister(); } super.onDestroy(); }