【问题标题】:How to stop Android activity opening from USB device being attached if activity is already open?如果活动已经打开,如何停止从连接的 USB 设备打开 Android 活动?
【发布时间】:2019-09-11 07:18:13
【问题描述】:

我正在使用 Android USB 主机,并打算在设备连接到手机时打开应用程序。但是,如果应用程序已打开,我不希望它再次打开。

以下是清单文件中的相关代码:

<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" />

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

如果应用程序已经打开,我有可以检测设备的 Java 代码,因此无需再次打开应用程序。在实践中,这只是一个烦恼。该应用程序已打开,并在其顶部打开另一个“副本”。

【问题讨论】:

    标签: android android-intent android-activity intentfilter usb-hostcontroller


    【解决方案1】:

    听起来您需要在活动的清单条目中设置一个标志。

    singleTasksingleInstance,取决于您的需要。请参阅the documentation 了解每个含义:

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多