在我的应用中,我有两个activities
,它们看起来像两个不同的应用程序(主屏幕上的单独图标)给用户。现在他们都有一个intent-filter监听插入的USB设备。问题是,当我在Activity A
并且我将USB设备插入平板电脑时,会自动启动Activity B
。
我想要实现的是:
Activity A -> insert USB -> stay in Activity A and do something with the USB
Activity B -> insert USB -> stay in Activity B and do something else with the USB
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mypackage.myapp"
android:versionCode="100"
android:versionName="1.0.0-dev" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/my_icon"
android:theme="@style/AppTheme" >
<activity
android:name="mypackage.ActivityA"
android:icon="@drawable/my_icon"
android:label="@string/ActivityA"
android:launchMode="singleTask"
android:screenOrientation="landscape" >
<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/lib_device_filter" />
</activity>
<activity
android:name="mypackage.ActivityB"
android:icon="@drawable/another_icon"
android:label="@string/ActivityB"
android:launchMode="singleTask"
android:screenOrientation="landscape" >
<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/lib_device_filter" />
</activity>
</application>
</manifest>
答案 0 :(得分:2)
我在项目中发现了同样的问题,我想与大家分享一下 实施解决方案
首先,我做了一个名为DumpActivity的活动。此活动只是实现了意图过滤器,以便在USB插入时进行监听,这就是全部。当活动启动时,立即完成以返回上一个活动。此时,上一个活动(示例中的活动A或B)可以与USB进行交互。
/**
* This activity have the intent filters of the USB (see AndroidManifest.xml).
*
* When it is the first time that the USB microphones are plugged, this activity is launched.
* This is a solution to disable the popup dialog when a USB device is connected.
*
* When this activity is launched, immediately it is destroyed to return to the previous
* activity
*/
public class DumpActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
super.onStart();
if (getIntent().getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
finish();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
finish();
}
}
}
然后,在清单中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mypackage.myapp"
android:versionCode="100"
android:versionName="1.0.0-dev" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-feature android:name="android.hardware.usb.device" />
<application
android:allowBackup="true"
android:icon="@drawable/my_icon"
android:theme="@style/AppTheme" >
<activity
android:name="mypackage.DumpActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/lib_device_filter">
</meta-data>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_DETTACHED"
android:resource="@xml/lib_device_filter">
</meta-data>
</activity>
<activity
android:name="mypackage.ActivityA"
android:icon="@drawable/my_icon"
android:label="@string/ActivityA"
android:launchMode="singleTask"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="mypackage.ActivityB"
android:icon="@drawable/another_icon"
android:label="@string/ActivityB"
android:launchMode="singleTask"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
注意1:意图过滤器必须仅在此活动中!
注意2:如果您需要在不同的活动中与USB进行交互,并且每次插入USB时都不想看到弹出对话框,此解决方案非常有用。
Marc G。