zoukankan      html  css  js  c++  java
  • 广播监听USB插入与拔出

    复制代码
    package com.joy.usbbroadcastreceiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "joy:";
        private USBBroadcastReceiver usbBroadcastReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.hardware.usb.action.USB_STATE");
            usbBroadcastReceiver = new USBBroadcastReceiver();
            registerReceiver(usbBroadcastReceiver, intentFilter);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (usbBroadcastReceiver != null) {
                unregisterReceiver(usbBroadcastReceiver);
            }
        }
    
        class USBBroadcastReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                if (intent.getAction().equals("android.hardware.usb.action.USB_STATE")) {
                    if (intent.getExtras().getBoolean("connected")) {
                        // usb 插入
                        Toast.makeText(MainActivity.this, "usb 插入", Toast.LENGTH_SHORT).show();
                        logi("usb 插入");
                    } else {
                        //   usb 拔出
                        Toast.makeText(MainActivity.this, "usb 拔出", Toast.LENGTH_SHORT).show();
                        logi("usb 拔出");
                    }
                }
            }
        }
    
    
        public void logi(String s) {
            Log.i(TAG, "joy99:" + s);
        }
    }
    复制代码
  • 相关阅读:
    CocosCreator-Widget,前后台切换
    Unity获取未激活游戏对象的方法 、坐标转换
    Mathf函数
    C# activeSelf、activeInHierarchy、SetActive、SetActiveRecursively
    C# 碰撞,射线,点击,周期函数等基本代码
    TCP/IP 协议栈
    笔记—《程序员自我修养》
    Container With Most Water 双指针法
    多线程服务器 编程模型
    c++ 高效并发编程
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7019907.html
Copyright © 2011-2022 走看看