我正在开发一个Android项目。我需要一点帮助。有人可以指导我如何检查Android设备中是否启用了USB端口。
先谢谢
答案 0 :(得分:1)
您需要将BroadCastReceiver与以下操作一起使用
<receiver android:name=".DetactUSB">
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
<action android:name="android.intent.action.UMS_DISCONNECTED" />
</intent-filter>
</receiver>
在BroadcastReceiver中,你可以像下面这样编码来检查,
public class DetactUSB extends BroadcastReceiver
{
private static final String TAG = "DetactUSB";
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
{
TextView textView = new TextView(context);
textView.setBackgroundColor(Color.MAGENTA);
textView.setTextColor(Color.BLUE);
textView.setPadding(10,10,10,10);
textView.setText("USB connected……….");
Toast toastView = new Toast(context);
toastView.setDuration(Toast.LENGTH_LONG);
toastView.setGravity(Gravity.CENTER, 0,0);
toastView.setView(textView);
toastView.show();
Log.i(TAG,"USB connected..");
}
if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
{
TextView textView = new TextView(context);
textView.setBackgroundColor(Color.MAGENTA);
textView.setTextColor(Color.BLUE);
textView.setPadding(10,10,10,10);
textView.setText("USB Disconnected……….");
Toast toastView = new Toast(context);
toastView.setDuration(Toast.LENGTH_LONG);
toastView.setGravity(Gravity.CENTER, 0,0);
toastView.setView(textView);
toastView.show();
}
}
}