關於android系統監控USB連結狀態
阿新 • • 發佈:2019-02-11
package com.android.internal.policy.impl; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import com.android.internal.R; //import com.mediatek.keyguard.Telephony.KeyguardDialogManager ; import android.view.WindowManager; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.content.BroadcastReceiver; import android.os.Handler; /** * Created by scq on 16-11-9. */ public class USBCurrent{ private Context mContext; //private Handler mHandler; private int mFrequency = 0; //private AlertDialog.Builder builder; private AlertDialog dialog = null; private static final int MAGIC_NUMBER_TEN = 10; //private KeyguardDialogManager mDialogManager; private float mCurrent; private int mPlugType; //充電型別 private int mElectricity; //電壓 private int mUSBStatus; //USB狀態 private String mTitle; private String mMessage; private String mBody; public USBCurrent(Context context){ mContext = context; //mDialogManager = KeyguardDialogManager.getInstance(context); /*if(mHandler == null){ mHandler = new Handler(); } mHandler.postDelayed(mRunnable, 500);*/ setIntentFilter(context); } public USBCurrent(Context context, float current, int electricity, int usbstatus){ mContext = context; mCurrent = current; mElectricity = electricity; mUSBStatus = usbstatus; //mDialogManager = KeyguardDialogManager.getInstance(context); setIntentFilter(context); //USBJudgMent(); } /** 讀取USB當前狀態 **/ public void setUSBCurrentStatus(float current, int electricity, int usbstatus){ mCurrent = current; mElectricity = electricity; mUSBStatus = usbstatus; //USBJudgMent(); } private void setIntentFilter(Context context){ IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); context.registerReceiver(mIntentReceiver, filter); } /** 廣播接收各狀態 **/ private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { /*if(mHandler == null){ mHandler = new Handler(); }*/ final String action = intent.getAction(); if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { mElectricity = (int)(100f * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100)); mCurrent = tenthsToFixedString(100 * intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)); mUSBStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); mPlugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); // boolean mCharged = status == BatteryManager.BATTERY_STATUS_FULL; // mUSBStatus = mCharged || status == BatteryManager.BATTERY_STATUS_CHARGING; android.util.Log.d("Alinscq"," mElectricity = " + mElectricity + " , mCurrent = " + mCurrent + " , mUSBStatus = " + mUSBStatus + " , mPlugType = " + mPlugType); USBJudgMent(); } } }; /*private Runnable mRunnable = new Runnable() { @Override public void run() { mFrequency ++ ; setIntentFilter(mContext); android.util.Log.d("Alinscq"," mFrequency = " + mFrequency); } };*/ private float tenthsToFixedString(int x) { int tens = x / MAGIC_NUMBER_TEN; return Float.parseFloat(new String("" + tens + "." + (x - MAGIC_NUMBER_TEN * tens))); } /*public void Dialog(){ if(builder == null) { builder = new AlertDialog.Builder(mContext); } builder.setMessage(R.string.dialog_title_body); builder.setTitle(R.string.dialog_title_prompt); builder.setNegativeButton(R.string.dialog_title_calera, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int index) { dialog.dismiss(); } }); }*/ /** 處理USB當前狀態,並監聽電流狀態彈出提示 **/ public void USBJudgMent(){ if(mUSBStatus == BatteryManager.BATTERY_STATUS_UNKNOWN){ /*mTitle = mContext.getString(com.android.internal.R.string.dialog_title_prompt); mMessage = mContext.getString(com.android.internal.R.string.dialog_title_body_link); mBody = mContext.getString(com.android.internal.R.string.dialog_title_calera); ShowDialog(mMessage, mTitle, mBody);*/ mFrequency = 0; android.util.Log.d("Alinscq"," mFrequency = 11111111"); }else{ if(!(mPlugType > 0)){ mFrequency = 0; }else{ mFrequency ++; android.util.Log.d("Alinscq"," mFrequency = " + mFrequency); if(mFrequency > 30){ mFrequency = 6; } if(mFrequency == 5){ if(mPlugType == BatteryManager.BATTERY_PLUGGED_AC){ mTitle = mContext.getString(com.android.internal.R.string.dialog_title_prompt); mMessage = mContext.getString(com.android.internal.R.string.dialog_title_body); mBody = mContext.getString(com.android.internal.R.string.dialog_title_calera); if(mCurrent < 650 && mElectricity < 95) { ShowDialog(mMessage, mTitle, mBody); } android.util.Log.d("Alinscq"," mFrequency = 2222222"); }else if(mPlugType == BatteryManager.BATTERY_PLUGGED_USB){ mTitle = mContext.getString(com.android.internal.R.string.dialog_title_prompt); mMessage = mContext.getString(com.android.internal.R.string.dialog_title_body_usb); mBody = mContext.getString(com.android.internal.R.string.dialog_title_calera); if(mCurrent < 650 && mElectricity < 95) { ShowDialog(mMessage, mTitle, mBody); } android.util.Log.d("Alinscq"," mFrequency = 333333"); }else if(mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS){ mTitle = mContext.getString(com.android.internal.R.string.dialog_title_prompt); mMessage = mContext.getString(com.android.internal.R.string.dialog_title_body); mBody = mContext.getString(com.android.internal.R.string.dialog_title_calera); if(mCurrent < 650 && mElectricity < 95) { ShowDialog(mMessage, mTitle, mBody); } android.util.Log.d("Alinscq"," mFrequency = 444444"); } } } } } /** 提示框 **/ private AlertDialog createDialog(String message, String title, String body) { final AlertDialog dialog = new AlertDialog.Builder(mContext) .setMessage(message) .setTitle(title) .setCancelable(false) .setNegativeButton(body, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //mDialogManager.reportDialogClose(); } }).create(); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);//TYPE_SYSTEM_ALERT return dialog; } public void ShowDialog(String message, String title, String body){ if(dialog == null){ dialog = createDialog(message, title, body); } dialog.show(); } /*public void HideDialog(){ AlertDialog dialog = createDialog(); dialog.hide(); }*/ public void setUSBStatus(int usbstatus){ mUSBStatus = usbstatus; } public int getUSBStatus(){ return mUSBStatus; } public void setCurrent(float current){ mCurrent = current; } public float getCurrent(){ return mCurrent; } public void setPlugType(int plugtype){ mPlugType = plugtype; } public int getPlugType(){ return mPlugType; } public void setElectricity(int electricity){ mElectricity = electricity; } public int getElectricity(){ return mElectricity; } }