I want to display Launcher app chooser dialog whenever I want but It is not displaying , app is getting closed.
This is my code :
getPackageManager().clearPackagePreferredActivities(getPackageName());
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
If I set phone launcher as default by pressing always then it will not display the dialog , I am really confused why it is happening.
it will not display the intent chooser in 2 different cases:
if there is only one launcher app present in the android device.
the current launcher app is set as default.
I dont know if there are any other cases as well.
There is a 3rd possibility: the android OS is modified and does not respect the standards. I have this problem on a Huawei T1-701u tablet.
This tab has an extra screen for app standard settings. So the app-settings will always state "no standard set" and yet you will never see a chooser as the standard is controlled by a propietary system.
Related
I wanted a way to exit my app. Hence I searched and found a piece of code which does that.
But I am not able to understand the code and why it does what it does.
Can anyone please explain?
Here is my code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It's starting the action ACTION_MAIN that has the category CATEGORY_HOME. This corresponds to:
This is the home activity, that is the first activity that is displayed when the device boots.
So, this means the launcher. So, this is a fancy way of starting the launcher app.
Also, the flag FLAG_ACTIVITY_NEW_TASK means it will be started on a new task, not the same as your app is running.
Notice that this will not finish your app, it will just put it in the background (like switching to another app using the task switcher, just this app happens to be the launcher). The effect is the same as pressing the home button.
Also notice that if the user happens to have two launcher apps, and it has not selected the default one, it will get a chooser asking to select which one of the two (or more) apps wants to use. Not the best experience.
I know how to launch an application using an intent and the application's package name.
Like this:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
if (launchIntent != null) {
startActivity(launchIntent);
}
What I want to do is that the user could choose from the applications installed on his/her device, not a specific one.
How can I do so?
Also, I would like to open the other app inside a floating window (maybe in custom alert dialog or equivalent if there is a possibility to do so)
How can I do so?
Use PackageManager and queryIntentActivities() to find all the activities that respond to the standard home screen launcher Intent structure (ACTION_MAIN and CATEGORY_LAUNCHER). Present those to the user (e.g., in a list). See this sample app.
I would like to open the other app inside a floating window (maybe in custom alert dialog or equivalent if there is a possibility to do so)
You would need to implement your own mobile operating system. Even Android 7.0's multi-window support does not support this, except in cases where the device is already in freeform multi-window mode (e.g., Chrome OS). You are welcome to use FLAG_ACTIVITY_NEW_TASK to launch the activity into another task, which will give it a separate window on freeform multi-window devices.
To exit an app programatically in Android (for instance, if the user presses an exit button), I use:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
However, the CATEGORY_HOME intent category is not supported when porting Android apps for Playbook or Blackberry 10. What should I use instead?
I think your "exit an app code" for your Android app is quite the opposite of exiting the app: You start a new activity for the home screen instead of finishing "your own" activity.
You should instead finish your activity like this (and consider if that applies to more than one activity on your stack):
myAppView.finish()
This would also "quit" your application on bb10 - which means, it will be minimized and shown as an active frame until you tip on the X to close it.
As well, it would be a good idea to decide if you should clear outstanding notifications at this time (for my app, this makes sense...)
Once my Home Screen app 'ABC' is installed on the device & when the user presses the Home Button, he is prompted with the Default Action Android dialog to choose between the Default Home & my ABC app.
My use case is to keep on prompting the user - a bit infrequently though, with this Default Action dialog till he selects my App as default.
How we can default action in android ?
Before ICS (and maybe Honeycomb), you could set the preferred activity using the PackageManager#addPreferredActivity method.
But as stated in the documentation :
This is a protected API that should not have been available to third party applications. It is the platform's responsibility for assigning preferred activities and this can not be directly modified.
This indeed could be used by Malware to change what applications the launcher icons start. I would really advise you not to use it and instead let your users the choice to make themselves your launcher their default launcher.
call the below intent when you need to see the 'launcher' selection dialog, unless one of the launcher apps is set as default
Intent homeIntent = new Intent("android.intent.action.MAIN");
homeIntent.addCategory("android.intent.category.HOME");
startActivity(homeIntent);
I'm building a home screen replacement app and was wondering how to launch the camera app in normal mode. The 'Home' sample does not have a camera app listed since its not installed as a separate app on the device (which explains why my galaxy nexus camera issues haven't been resolved). In otherwords, I want to launch the camera app in the same way I can do so from my stock home screen launcher...
you would probably want to open an intent from a Component name with the Main intent instead of the usual method of calling the camera for an answer.
You can open any activity on Android as if you are clicking on it in the home screen you just need the compentent name, you can get the component name by opening it normally as a user then reading it off your logcat when the activity opens... I have done the Camera intent for you.
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("com.google.android.camera/com.android.camera.Camera"));
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);