2012-04-18 211 views
0

我想在我的android應用程序中彈出一個usb設備連接和分離時的敬酒..
短 - 它不工作..有人可以告訴我我失蹤了嗎?試圖三星硬鍵盤連接到samasung平板電腦,並有我的應用程序檢測到它在安裝或取下檢測USB設備android

我的活動

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> 

+0

你有沒有得到一些解決方案,你可以分享? – 2013-02-14 23:46:21

回答

2

EDIT2:

想如果我沒有記錯的三星硬鍵盤連接到三星平板

:問題是,鍵盤是不是一個USB Acessory,因此應不會觸發USB_ACCESSORY_ATTACHED部分。

忘記下面:)


部分嘗試

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

更換

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); 

,並刪除

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

您正在混淆具有廣播意圖的權限。

編輯:很可能這是完全錯誤的:http://developer.android.com/guide/topics/usb/accessory.html做你所做的。

+0

我做了您的更改,但仍然沒有任何結果。 – erik 2012-04-18 18:16:06

+1

請參閱頂部的「新」答案。配件有些特別,我想你的鍵盤沒有配件(它需要有電池/電源本身),所以你什麼也得不到。 – zapl 2012-04-18 18:19:22