且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查是否在Android上启用了USB连接模式?

更新时间:2023-12-04 19:34:46

这是类似于此:的在Android 检查USB的连接状态

This is similar to this: Check USB Connection Status on Android

虽然他们利用广播接收机。你说这是一个做事的笨方法,但你可以具体谈谈吗?

Although they do make use of Broadcast Receivers. You say this is a stupid way of doing things, but can you be more specific about that?

这并不是说你不能检测到它的应用程序启动后的情况。你需要做的是把

It is not the case that you can't detect it after the app has started. What you would need to do would be to put

<intent-filter>
<action android:name="android.intent.action.ACTION_UMS_CONNECTED"/>
<action android:name="android.intent.action.ACTION_UMS_DISCONNECTED"/>

在AndroidManifest.xml中下方的&lt;接收&GT; 项下面的类:

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一>