0

当连接和分离 USB 设备时,我试图在我的 android 应用程序中弹出一个 toast ..
短 - 它不工作.. 有人可以告诉我我错过了什么吗?尝试将三星硬键盘连接到三星平板电脑并让我的应用在附加或分离时检测到它

我的活动

    import java.util.ArrayList;
    import java.util.HashMap;

    import com.eliddell.R;
    import android.app.Activity;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.res.Configuration;
    import android.hardware.usb.UsbAccessory;
    import android.hardware.usb.UsbDevice;
    import android.hardware.usb.UsbManager;
    import android.location.GpsStatus.Listener;
    import android.mtp.MtpDevice;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Toast;

    public class UiModeTestActivity extends Activity {
        /** Called when the activity is first created. */
        //UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            String str = (String) getLastNonConfigurationInstance();

            IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
registerReceiver(mUsbReceiver, filter);

        }


       private static final String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION";
       private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

            public void onReceive(Context context, Intent intent) {
                Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
                String action = intent.getAction();
                if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                    UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    Toast.makeText(getApplicationContext(), "attached", Toast.LENGTH_SHORT).show();
                }
                if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
                    UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    Toast.makeText(getApplicationContext(), "detached", Toast.LENGTH_SHORT).show();
                }
            }
        };

    };

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="12" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"

    >
    <uses-library android:name="com.android.future.usb.accessory" /> 
    <activity
        android:name=".UiModeTestActivity"
        android:configChanges="keyboard|uiMode|orientation"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
            android:resource="@xml/accessory_filter" />
    </activity>

</application>

4

1 回答 1

2

编辑2:

试图将三星硬键盘连接到三星平板电脑

如果我没记错的话:问题是键盘不是 USB 附件,因此不应触发 USB_ACCESSORY_ATTACHED 部分。

忘记下面的部分:)


尝试更换

 UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
//final String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION";

PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);

IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
registerReceiver(mUsbReceiver, filter);

并删除

<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />

从你的 AndroidManifest.xml

您将权限与广播意图混淆了。

编辑:嗯,这可能是完全错误的:http: //developer.android.com/guide/topics/usb/accessory.html做了你所做的。

于 2012-04-18T18:02:39.570 回答