我在 android 市场上有一个应用程序,用户经常将他们的设备连接到他们的计算机以将音乐添加到应用程序文件夹中。我在说明中特别指出,启用usb连接模式后,android应用程序无法与sd卡通信。显然,这还不够愚蠢。有没有办法在 java 中检测 USB 是否已连接?我已经看到它是用广播接收器完成的,但这是一种非常愚蠢的方法。如果用户在连接到 USB 后启动应用程序,这将不起作用。一定有更好的方法。
最佳答案
这类似于:Check USB Connection Status on Android
尽管他们确实使用广播接收器。你说这是一种愚蠢的做事方式,但你能更具体一点吗?
不是说应用启动后检测不到。你需要做的是把
<intent-filter>
<action android:name="android.intent.action.ACTION_UMS_CONNECTED"/>
<action android:name="android.intent.action.ACTION_UMS_DISCONNECTED"/>
在<Receiver>
下的AndroidManifest.xml 中以下类(class)的条目:
public class IntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(Intent.ACTION_UMS_CONNECTED)){
Toast.makeText(context, "mounted", Toast.LENGTH_LONG).show();
Intent myStarterIntent = new Intent(context, tst.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
}
`
引用来源:http://www.anddev.org/start_activity_on_usb_connect-t8814.html
关于android - 如何检查android上是否启用了USB连接模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8797792/