I want to remove my app from list of apps and the recent apps list. So I tried to disable my main / launcher activity with the following code:
ComponentName componentToDisable = new ComponentName(context, MainActivity.class);
context.getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This does the job. But now I try to reinstall the app and it fails saying "activity MainActivity class doesn't exist". If I uninstall the app, the installation works again. How can I handle this issue? Thanks a lot for your time and help
I found that I have to make the activity enabled before reinstalling it.
This can be done by having a broadcast receiver listen to package_add / remove events and in onReceive make the activity enabled again.
public void onReceive(Context context, Intent intent) {
Log.i("Receiver","got event");
ComponentName componentToDisable = new ComponentName(context,BlockableComponentActivity.class);
context.getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
Manifest excerpt for the receiver:
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
Related
I have 2 activities within my app. Say FirstActivity and SecondActivity. FirstActivity is the MAIN and LAUNCHER activity. SecondActivity uses usb devices. I want the prompt for USB permission to appear only once within lifetime of the app.
If there was only one Activity, the following lines in manifest solved my purpose:
<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>
This was doing the following:
Launching FirstActivity whenever usb device (mentioned in xml) is connected is app is not already open.
Prompt for usb device permission appears only once.
How do I modify this to achieve the following:
If SecondActivity is already running and a new usb device is attached, I must be able to use the device without relaunching the app. So I have written a broadcast receiver for the same as follows:
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.
}
}
But the problem is FirstActivity gets relaunched again when usb device is attached while SecondActivity is running. How do I avoid this ?
Will add more information if needed. Would be thankful for any help.
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-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) add this code to onCreate() (or to onResume()) of SecondActivity
PackageManager pm = getApplicationContext().getPackageManager();
ComponentName compName =
new ComponentName(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:
#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(), 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:
PackageManager pm = getApplicationContext().getPackageManager();
ComponentName compName =
new ComponentName(getPackageName(), getPackageName() + ".AliasFirstActivity");
pm.setComponentEnabledSetting(
compName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
But the problem is FirstActivity gets relaunched again when usb device is attached while SecondActivity 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:
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" android:launchMode="singleTask">
<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>
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.
This answer might be helpful:
USB device access pop-up suppression?
Code snippet:
public class UsbEventReceiverActivity extends Activity
{
public static final String ACTION_USB_DEVICE_ATTACHED = "com.example.ACTION_USB_DEVICE_ATTACHED";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
protected void onResume()
{
super.onResume();
Intent intent = getIntent();
if (intent != null)
{
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
{
Parcelable usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// Create a new intent and put the usb device in as an extra
Intent broadcastIntent = new Intent(ACTION_USB_DEVICE_ATTACHED);
broadcastIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
// Broadcast this event so we can receive it
sendBroadcast(broadcastIntent);
}
}
// Close the activity
finish();
}
}
I have an app which is kept hidden from the launcher. Now I want to open the app from the dialer. So I have a BroadcastReceiver where I am handling things.
1) First I show the app,
ComponentName componentName = new ComponentName(context,
SplashActivity.class);
context.getPackageManager().setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
2) Then launch an Intent,
Intent launcher = new Intent(context, SplashActivity.class);
launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(launcher);
3) Then I am hiding the app again,
ComponentName componentName2 = new ComponentName(context,
SplashActivity.class);
context.getPackageManager().setComponentEnabledSetting(
componentName2,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
The Problem
The Intent launch is successful but the app is getting killed after a few seconds. But the problem doesn't occur if I launch any other activity other than the SplashActivity. What is the problem and how can it be solved?
You can use <activity-alias> tag in Android manifest for launcher activity. change in your manifest for launcher activity like as below:-
<activity-alias
android:name="com.watever.SplashActivityAlias"
android:targetActivity="com.watever.SplashActivity"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity-alias>
and in code where where you are showing and hiding app just use Alias Name which is used in manifest like in above manifest alias name is SplashActivityAlias so use SplashActivityAlias instead of SplashActivity in both conditions where you are showing and hiding app.
By doing above thing you can able to hide your app icon with
setComponent EnabledSetting method using Alias name and you can launch your main activity which is splash activity by using dialer
For hiding app
ComponentName componentName2 = new ComponentName("com.packagename",
"com.packagename.HideAppActivity");
context.getPackageManager().setComponentEnabledSetting(
componentName2,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
We have an app that scans for Bluetooth devices. The code responsible for scanning should only run when bluetooth is enabled. Also we want to disable/enable this feature at any point in time.
We choose to implement a BroadcastReceiver that registers for the BluetoothAdapter.ACTION_STATE_CHANGED broadcast.
Here some of the problems we encountered:
Programmatically enable the BroadcastReceiver:
public void registerForBroadcasts(Context context) {
IntentFilter bluetooth = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
context.registerReceiver(this, bluetooth);
}
When the app is killed, the BroadcastReceiver is also not active anymore. So if the user multi-tasks-swipes the app to death, it is not being woken up again.
We have full control, when to start the BroadcastReceiver.
Declare the BroadcastReceiver in the Manifest
<receiver android:name="com.mypackage.BroadcastReceiver">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
</receiver>
The BroadcastReceiver is active right after the app start.
The BroadcastReceiver cannot be disabled.
Declare the BroadcastReceiver in the Manifest as disabled + enable it programmatically
<receiver android:name="com.mypackage.BroadcastReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
</receiver>
Then enable the component if you need it.
public void registerForBroadcasts(Context context) {
ComponentName component = new ComponentName(context, BroadcastReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
My tests have shown, the state is persisted with the system, so the BroadcastReceiver will stay enabled. It combines the advantages of both methods.
BroadcastReceiver can be disabled
BroadcastReceiver can be on or off by default
BroadcastReceiver keeps activation even if app is disabled and phone rebooted.
Am I missing something, does this method seem legit?
It is possible to enable/disable the receiver programatically.
To Enable programatically
PackageManager pm = Re_editActivity.this.getPackageManager();
ComponentName componentName = new ComponentName(currentActivity.this, name_of_your_receiver.class);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
To Disable programatically
PackageManager pm = Re_editActivity.this.getPackageManager();
ComponentName componentName = new ComponentName(currentActivity.this, name_of_your_receiver.class);
pm.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Manifest receiver
<receiver android:name="name_of_your_receiver" android:enabled="false">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The BroadcastReceiver cannot be disabled.
Sure it can. Use PackageManager and setComponentEnabledSetting(), as you did in your third scenario.
Am I missing something, does this method seem legit?
It is very legit, at least in terms of managing the BroadcastReceiver. I don't know if there are any Bluetooth-specific hiccups, though I would doubt it. This technique is used for various broadcasts, such as only listening to ACTION_BOOT_COMPLETED when you really do have something that needs to be done at boot time.
I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?
This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.
You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));
I'm writing an app updater for my app. After I make sure I have my apk on the device, this is what I do from within the app I'm trying to update:
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
File f = new File(apkLocation);
promptInstall.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
_context.startActivity(promptInstall);
This launches my installer which displays the app permissions and I am able to click "Install". But from here the app simply closes, I get no message whatsoever (I would've expected the dialog telling me the install was successful giving me the option to press "Close" or "Open"). It just goes to the main screen of the device without further notice.
On a side note, the app is indeed updated when I manually open it back.
How can I make the installer go all the way as expected? Is there anything to set on the intent?
While writing this, I'm wondering if the reason this happens is that the current app is simply overwritten on the device thus closing it and by extent not getting the result of the intent because it's source was killed?
All you can do is register a receiver with the intent filters like android.intent.action.PACKAGE_INSTALL or android.intent.action.PACKAGE_REPLACED from which you can restart your application back again.
<receiver android:enabled="true" android:exported="true" android:label="BootService" android:name="com.project.services.BootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
And
public class BootService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context,Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
}
}
To successfully update you need to launch intent with URI indicating to your update app as new task.
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK));
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
My post below:
Android application update issue
First off, you can't install without prompt, unless you are rooted or have system privileges. I don't think you were asking that, but one of your paragraphs isn't clear.
Secondly, if installing an update version of a running app, the behavior you're seeing is expected: The app is force-closed and updated. You can't update in-place. You can detect when the installation was aborted, because the activity invoking the installer will be resumed.
In order to update a running app AND keep it running, you'll need a separate process (app) to monitor the installation and restart your app.