如果只有一个Activity,则清单中的以下行解决了我的目的:
<activity android:name=".FirstActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <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/usb_device_filter" /> </activity>
这是在做以下事情:
>每当usb设备(在xml中提到)连接时启动FirstActivity,应用程序尚未打开.
>提示usb设备权限仅出现一次.
如何修改此项以实现以下目标:
>如果SecondActivity已经运行并且连接了新的USB设备,我必须能够在不重新启动应用程序的情况下使用该设备.所以我写了一个广播接收器,如下所示:
public class TriggerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context,Intent intent) { if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { read connected usb devices and register serial port call listener back. } }
但问题是当SecondActivity运行时附加USB设备时,FirstActivity再次重新启动.我该如何避免这种情况?
如果需要,将添加更多信息.感谢任何帮助.
解决方法
更新
FirstActivity在每个USB_DEVICE_ATTACHED(甚至是SecondActivity正在运行)上触发,因为您设置了< action android:name =“android.hardware.usb.action.USB_DEVICE_ATTACHED”/>在AndroidManifest.xml文件中过滤它.因此,您应该在SecondActivity运行时禁用此过滤器.你可以这样做:
1)在AndroidManifest.xml中将(例如AliasFirstActivity)添加到FirstActivity并移动< action android:name =“android.hardware.usb.action.USB_DEVICE_ATTACHED”/>过滤到别名描述(您应该从FirstActivity描述中删除< action android:name =“android.hardware.usb.action.USB_DEVICE_ATTACHED”/>):
<activity-alias android:targetActivity=".FirstActivity" android:name=".AliasFirstActivity" android:label="@string/app_name"> <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-alias>
2)将此代码添加到SecondActivity的onCreate()(或onResume())
PackageManager pm = getApplicationContext().getPackageManager(); ComponentName compName = new ComponentName(getPackageName(),getPackageName() + ".AliasFirstActivity"); pm.setComponentEnabledSetting( compName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
抑制针对FirstActivity的意图过滤器USB_DEVICE_ATTACHED.你应该在SecondActivity中有类似的东西:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); triggerReceiver = new TriggerReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); registerReceiver(triggerReceiver,filter); PackageManager pm = getApplicationContext().getPackageManager(); ComponentName compName = new ComponentName(getPackageName(),PackageManager.DONT_KILL_APP); }
这应该解决你的问题.
3)如果需要,你可以恢复< action android:name =“android.hardware.usb.action.USB_DEVICE_ATTACHED”/>使用此代码过滤SecondActivity的onDestroy()(或onPause())中的FirstActivity:
PackageManager pm = getApplicationContext().getPackageManager(); ComponentName compName = new ComponentName(getPackageName(),getPackageName() + ".AliasFirstActivity"); pm.setComponentEnabledSetting( compName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);