Android监听USB连接/断开,充电/未充电




    
        

    
package zhangphil.adb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends AppCompatActivity {
    private String TAG = "USB连接状态";
    private final static String USB_ACTION = "android.hardware.usb.action.USB_STATE";

    private TextView text;
    private String string = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.text);

        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);
        filter.addAction(USB_ACTION);
        registerReceiver(mBroadcastReceiver, filter);

        registerReceiver(mBroadcastReceiver, new IntentFilter(USB_ACTION));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    private void displayMsg(String s) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss:SSS");
        string = string + sdf.format(new Date()) + "\t\t" + s + "\n";
        text.setText(string);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case USB_ACTION:
                    boolean connected = intent.getExtras().getBoolean("connected");
                    if (connected) {
                        displayMsg("USB已连接");
                    } else {
                        displayMsg("USB未连接");
                    }
                    break;

                case Intent.ACTION_BATTERY_CHANGED:
                    //电量发生改变。
                    displayMsg("电量发生改变");

                    boolean isCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
                    if (isCharging) {
                        //剩余电量。
                        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

                        //电量最大值。
                        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

                        //电量百分比。
                        float batteryPct = level / (float) scale;
                        displayMsg("充电," + level + "-" + batteryPct + "-" + scale);
                    }
                    break;

                case Intent.ACTION_BATTERY_LOW:
                    displayMsg("电量过低");
                    break;

                case Intent.ACTION_BATTERY_OKAY:
                    displayMsg("电量满");
                    break;

                case Intent.ACTION_POWER_CONNECTED:
                    displayMsg("电源接通");
                    break;

                case Intent.ACTION_POWER_DISCONNECTED:
                    displayMsg("电源断开");
                    break;
            }
        }
    };
}

 

你可能感兴趣的:(Android)