处理不同活动的同一个意图过滤器 [英] Handle same intent-filter for different activity

查看:56
本文介绍了处理不同活动的同一个意图过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有两个activities,它们对用户来说像两个不同的应用程序(主屏幕上的单独图标).现在,它们两个都有一个意图过滤器,用于侦听插入的USB设备.问题是,当在Activity A中并且我将USB设备插入平板电脑时,会自动启动Activity B. 我想要实现的是:

In my app I have two activities which appear like two different apps (separate icons on the homescreen) to the user. Now both of them have an intent-filter listening for USB-devices plugged in. The problem is, that when in Activity A and I insert the USB-device to the tablet, automatically Activity B starts. What I want to achieve is:

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>

推荐答案

我在一个项目中发现了同样的问题,并希望与您分享 已实施的解决方案.

I found with the same issues in a project and I would like to share with you the implemented solution.

首先,我进行了一个名为DumpActivity的活动.此活动仅实现意图过滤器以在插入USB时进行监听,仅此而已.启动活动后,立即完成返回上一个活动的操作.此时,先前的活动(在您的示例中为活动A或B)可以与USB交互.

First of all, I made a activity called DumpActivity. This activity just implement the intent filters to listen when the USB is plugged, that's all. When the activity is launched, immediatly it's finished to return to the previous activity. At this point, the previous activity (Activity A or B in your example) is able to interact with the 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:意图过滤器必须仅在此活动中!

NOTE 1: The intent filter must be only in this activity!

注2:如果您需要在不同的活动中与USB交互并且不想在每次插入USB时都看到弹出对话框,则此解决方案很有用.

NOTE 2: This solution is useful if you need to interact with the USB in different activities and you don't want to see the popup dialog everytime you plug the USB.

马克·G.

这篇关于处理不同活动的同一个意图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆