私は独自の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>