首页 > 代码库 > 广播监听USB插入与拔出
广播监听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); } }
广播监听USB插入与拔出
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。