2016-12-30 48 views
1

私は独自のUSBデバイス(ホストモードではアンドロイド)を使用するアプリケーションを持っています。Android USBホスト、singleInstance、onCreateがUSB接続で呼び出され続ける

私はすべてが、デバイスが取り外され、再付着されるたびに、エンドユーザーがアクセス権を要求されていることを除いて、素晴らしい作品

// this block is in the constructor 
usbConnectionReceiver = new UsbConnectionReceiver(); 
IntentFilter filter = new IntentFilter(); 
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); 
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); 
context.registerReceiver(usbConnectionReceiver, filter); 



public class UsbConnectionReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { 
      UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 
      if (device != null) onDeviceAttached(device); 
     } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { 
      UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 
      if (device != null) onDeviceDetached(device); 
     } 
    } 
} 

public void onDeviceAttached(UsbDevice device) { 
    usbDevice = device; 
    intf = usbDevice.getInterface(0); 
    endpoint = intf.getEndpoint(0); 
    if (usbManager.hasPermission(usbDevice)) { 
     usbDeviceConnection = usbManager.openDevice(usbDevice); 
     boolean status = usbDeviceConnection.claimInterface(intf, true); 
     if (status) { 
      startPollingThread(); 
     } 
    } else { 
     // I used to have code here that would programmatically prompt for permissions. 
    } 
} 

コンストラクタで登録BroadcastReceiverを持っています。 「このUSBデバイスにデフォルトで使用する」チェックボックスは完全に無視されます。

いくつかの調査の後で、私がコードを介してパーミッションを要求すると、それは事実です。

私はインテントフィルタを使用するように自分の活動を切り替えました。私はもはや初めてのアクセス許可ボックスを取得しないので、チェックボックスが働いています。代わりに、デバイスが再接続されるたびに、それは私のアプリのonCreate()を呼び出し、クラッシュします。

私が持っているlaunchMode="singleInstance"(実際には、私はすべての品種を試してみた)、そしてアプリはとランニングとトップが画面上にアップしている間、onCreate()ではなく、ドキュメントが示唆するように思わonNewIntent()(と呼ばれるべきです起こる)。

また、私は、私も、私は無駄にandroid:taskAffinity=""

すべてを設定する必要が読んandroid:launchMode="singleInstance" を設定する必要があることを読みました。私は類似のポストのトンがあることを知っている、しかしそれらのどれも働くように思わない。アドバイスをいただければ幸いです。ここで

は私ののAndroidManifest.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.me.myapp" > 
    <application 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" 
     android:launchMode="singleInstance" > 
     <activity 
      android:name=".MainActivity" 
      android:taskAffinity="" 
      android:label="@string/app_name" 
      android:configChanges="keyboard" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
      </intent-filter> 
      <meta-data 
       android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
       android:resource="@xml/device_filter" /> 
     </activity> 
    </application> 
</manifest> 

答えて

0

私はこれが最善の解決策であると信じて...誰もがコメントを持っている場合はアンドロイドでUSB取り扱いは常に持っているので、私はそれを聞いてみたいです頭痛になりました!

私は次のアクティビティ

public class UsbActivity extends Activity { 
    private static final String TAG = "UsbActivity"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     Intent intent = getIntent(); 
     if (intent != null) 
     { 
      if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) 
      { 
       Logger.lc(TAG,"ACTION_USB_DEVICE_ATTACHED"); 
       UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 
       Logger.lc(TAG,"UsbDevice"); 
       UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
       Logger.lc(TAG,"UsbManager"); 
       if (usbDevice != null && usbManager != null && !usbManager.hasPermission(usbDevice)) { 
        Logger.lc(TAG,"openDevice"); 
        usbManager.openDevice(usbDevice); 
       } 
      } 
     } 
     finish(); 
    } 
} 

を作成し、以下style.xml

でマニフェスト

<activity 
     android:name="com.myapp.UsbActivity" 
     android:label="MyApp" 
     android:theme="@style/Theme.Transparent" 
     android:noHistory="true" 
     android:excludeFromRecents="true" 
     android:taskAffinity="com.myapp.taskAffinityUsbReceiver" 
     android:process=":UsbEventReceiverActivityProcess" 
     android:exported="false" > 
     <intent-filter> 
      <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
      android:resource="@xml/device_filter" /> 
</activity> 

にこれを追加基準としてUSB device access pop-up supression?

このポストを使用

<style name="Theme.Transparent" parent="android:Theme"> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:windowBackground">@android:color/transparent</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="android:windowNoTitle">true</item> 
     <item name="android:windowIsFloating">true</item> 
     <item name="android:backgroundDimEnabled">false</item> 
     <item name="android:windowAnimationStyle">@null</item> 
    </style> 
関連する問題