当android设备连接到USB时,会发送android.hardware.usb.action.USB_STATE
Lz 可以在AnroidManiFest.xml中声明如下   接受该广播
        <receiver android:name=".UsbReceiver">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_STATE"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                <data android:scheme="file"/>
            </intent-filter>
        </receiver>在增加UsbReceiver.java文件
对接受到的广播做处理public class UsbReceiver extends BroadcastReceiver {
    private static final String TAG = "UsbReceiver";
    private static final String ASK_ON_PLUG = "ask_on_plug";
    private static final String sInecm = SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE, "false");
    private StorageManager mStorageManager = null;
    private UsbManager mUsbManager;
    private static boolean mMeidaMountStatus;
    private SharedPreferences mSharedPre;
    private SharedPreferences.Editor mEeditor;
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        mUsbManager = (UsbManager)context.getSystemService(Context.USB_SERVICE);
        mSharedPre = context.getSharedPreferences("com.android.settings_preferences", 0);
        mEeditor = mSharedPre.edit();
        if (UsbManager.ACTION_USB_STATE.equals(action) ){
            onUsbStateReceive(context, intent);
        } else if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
            onMediaMountedReceive(context, intent);
        }
    }    private void onMediaMountedReceive(Context context, Intent intent) {
        boolean usb_connected = mSharedPre.getBoolean("usbState", false);
        
        if(usb_connected && context.getResources().getBoolean(R.bool.usb_mass_storage_mode_enable)
            && UsbManager.USB_FUNCTION_MASS_STORAGE.equals(mUsbManager.getDefaultFunction())) {
            popUsbDialog(context);
        } else {
            return;//do nothing
        }
    }
    
    private void popUsbDialog(Context context) {
     boolean usb_connected = mSharedPre.getBoolean("usbState", false);
     String sdcardState = Environment.getSDCardStorageState();
        if (usb_connected
         && (sdcardState.equals(Environment.MEDIA_MOUNTED) || sdcardState.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
                && UsbManager.USB_FUNCTION_MASS_STORAGE.equals(mUsbManager.getDefaultFunction())
                && !mStorageManager.isUsbMassStorageEnabled()
                && (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) == 1)
                && context.getResources().getBoolean(R.bool.usb_mass_storage_mode_enable) ) {
            Intent i = new Intent();
            i.setClassName(context, "com.android.settings.MassStorageDialogActivity");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
    
    private void onUsbStateReceive(Context context, Intent intent) {
        boolean usb_connected = intent.getBooleanExtra(UsbManager.USB_CONFIGURED, false);
        final ContentResolver cr = context.getContentResolver();
        mEeditor.putBoolean("usbState", usb_connected);
        mEeditor.commit();        boolean changed = false;
        boolean preState = mSharedPre.getBoolean("preState", false);
        String progress = SystemProperties.get("vold.encrypt_progress");
        if ((progress != null) && !progress.isEmpty())  {
            return;
        }
        if (usb_connected != preState) {
            changed = true;
        }
        KeyguardManager kgm =
            (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        if (sInecm.equals("false")
                && usb_connected && changed && !preState
                && (1 == Settings.System.getInt(cr, UsbReceiver.ASK_ON_PLUG, 1))
                && !kgm.inKeyguardRestrictedInputMode()) {
            mEeditor.putBoolean("preState", true);
            mEeditor.commit();
            //注意这里,我这里处理的方式是当USB连接上了android设备,我就跳转到设置的UsbSettings界面,LZ可以参考我的代码自己来修改           
            context.startActivity(Intent.makeRestartActivityTask(
               new ComponentName("com.android.settings","com.android.settings.UsbSettings")));
        } else if (!usb_connected) {
            mEeditor.putBoolean("preState", false);
            mEeditor.commit();
        }
        String usbType = SystemProperties.get("persist.sys.usb.config", "");
        if (usbType != null) {
            UsbSettings.writeSuitestate(mUsbManager.getDefaultFunction());
        }
              
        if (UsbManager.USB_FUNCTION_MASS_STORAGE.equals(mUsbManager.getDefaultFunction())
            && context.getResources().getBoolean(R.bool.usb_mass_storage_mode_enable)) {
            if (usb_connected){
                popUsbDialog(context);
            } else {
             if (mStorageManager.isUsbMassStorageEnabled()) {
             mStorageManager.disableUsbMassStorage();
             }
            }
        }
    }
}

解决方案 »

  1.   

    if(BuildConfig.DEBUG)
    难道是说这句?
      

  2.   


    这个挺好,但是会有个问题,如果我一开始就是链接USB(默认开发模式运行),无法接受到广播的 ?
      

  3.   


    这个挺好,但是会有个问题,如果我一开始就是链接USB(默认开发模式运行),无法接受到广播的 ? 
      

  4.   

    ...不管插拔 USB数据线  都会发送android.hardware.usb.action.USB_STATE广播的如果你的应用  是预置到android 设备上的话  肯定是能接受到的
      

  5.   


    你好,我是开发模式,应用是我在Debug或者Run的时候安装的,这样是有问题啊
      

  6.   


    你好,我是开发模式,应用是我在Debug或者Run的时候安装的,这样是有问题啊
    如果不是事先预置安装的应用   确实会有问题     对于上叙提到的问题  暂时没有想到什么号的办法了~~先帮你顶贴    看其他人的想法是怎么个样子的
      

  7.   

    可以在注册文件里面写明,debugable=false