Usb Permissions Without Prompt
Solution 1:
Try "remove" intent filter <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
from FirstActivity
like in this question
Update
FirstActivity triggers on every USB_DEVICE_ATTACHED
(even SecondActivity
is running) because You set <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
filter for it in AndroidManifest.xml
file. So You should disable this filter when SecondActivity
running. You can do this by that way:
1) add (e.g. AliasFirstActivity
) in AndroidManifest.xml
to your FirstActivity
and move <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
filter to alias description (You should remove <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
from FirstActivity
description) like that:
<activity-aliasandroid:targetActivity=".FirstActivity"android:name=".AliasFirstActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter><intent-filter><actionandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /></intent-filter><meta-dataandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"android:resource="@xml/device_filter" /></activity-alias>
2) add this code to onCreate()
(or to onResume()
) of SecondActivity
PackageManagerpm= getApplicationContext().getPackageManager();
ComponentNamecompName=newComponentName(getPackageName(), getPackageName() + ".AliasFirstActivity");
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
to suppress intent filter USB_DEVICE_ATTACHED
for FirstActivity
. You should have in SecondActivity
something like that:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
triggerReceiver = newTriggerReceiver();
IntentFilterfilter=newIntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
registerReceiver(triggerReceiver, filter);
PackageManagerpm= getApplicationContext().getPackageManager();
ComponentNamecompName=newComponentName(getPackageName(), getPackageName() + ".AliasFirstActivity");
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
That should solve Your issue.
3) if it is needed, You can restore <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
filter for FirstActivity
in onDestroy()
(or in onPause()
) of SecondActivity
by using this code:
PackageManagerpm= getApplicationContext().getPackageManager();
ComponentNamecompName=newComponentName(getPackageName(), getPackageName() + ".AliasFirstActivity");
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Solution 2:
But the problem is
FirstActivity
gets relaunched again when usb device is attached whileSecondActivity
is running. How do I avoid this?
This is not hard to answer. In your AndroidManifest.xml you literally declare that your FirstActivity
should be launched when the event android.hardware.usb.action.USB_DEVICE_ATTACHED
occurs.
If you want to handle this event in SecondActivity
only, then you have to declare it in the manifest accordingly, for example:
<activityandroid:name=".FirstActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".SecondActivity"android:launchMode="singleTask"><intent-filter><actionandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /></intent-filter><meta-dataandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"android:resource="@xml/device_filter" /></activity>
Now, whenever your USB device is plugged in, SecondActivity
will be launched only. If SecondActivity
is already running, then it won't be launched again (or multiple times) because of the attribute android:launchMode="singleTask"
specified for SecondActivity
. You can read more about the different launch modes here if you're interested.
Since you declared in your manifest that SecondActivity
should be launched when a USB device is plugged in, the Android system will ask you the following question:
After ticking the checkbox "Use by default for this USB device", it won't ask you again. Now, everytime you plug in the USB device, your SecondActivity
will be launched and it will automatically receive the required USB permissions too.
Let me know if that answers your question.
Solution 3:
This answer might be helpful: USB device access pop-up suppression?
Code snippet:
publicclassUsbEventReceiverActivityextendsActivity
{
publicstaticfinalStringACTION_USB_DEVICE_ATTACHED="com.example.ACTION_USB_DEVICE_ATTACHED";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@OverrideprotectedvoidonResume()
{
super.onResume();
Intentintent= getIntent();
if (intent != null)
{
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
{
ParcelableusbDevice= intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// Create a new intent and put the usb device in as an extraIntentbroadcastIntent=newIntent(ACTION_USB_DEVICE_ATTACHED);
broadcastIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
// Broadcast this event so we can receive it
sendBroadcast(broadcastIntent);
}
}
// Close the activity
finish();
}
}
Post a Comment for "Usb Permissions Without Prompt"