如何检查是否在Android上启用了USB连接模式? [英] How to check if USB connected mode is enabled on android?

查看:455
本文介绍了如何检查是否在Android上启用了USB连接模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Android Market上的应用程序和用户的往往是他们的设备连接到他们的电脑上的音乐添加到应用程序文件夹中。我虽然启用了USB连接模式Android应用无法与SD卡的通信指令都明确表示。显然,这不傻足以证明。有没有办法在Java中检测是否该USB连接?我已经看到了与广播接收器完成的,但是这是这样做的一个笨方法。如果用户连接到USB后,启动应用程序,这将不起作用。有一定是更好的方法。

I have an app on the android market and users often connect their devices to their computers to add music into the apps folder. I have specifically stated in the instructions that android apps cannot communicate with the sd card while usb connected mode is enabled. Apparently this is not stupid proof enough. Is there a way to detect in java if the USB is connected? I have seen it done with broadcast receivers, but this is such a stupid way to do it. This won't work if the user starts the app after connecting to USB. There has got to be a better way.

推荐答案

这是类似于此:的在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

这篇关于如何检查是否在Android上启用了USB连接模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆