How to set Default action in Android from coding? - android

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);

Related

Changing default behaviour of wifi icon - Android

I have an app idea and have a simple question, can I change the behaviour of wifi item on quick settings menu on Android? I want it to redirect to my app. Is this possible?
Not generally.
You can examine Logcat and see what happens when the user taps that tile. If the tile starts an activity (probably yes) and the Intent that is used is implicit (action but no ComponentName or package), then you could create an activity with a matching <intent-filter>. The user would be able to choose to launch your activity instead of the standard one.
However, bear in mind that there is no requirement that the Intent that the tile uses would be the same across Android versions and device manufacturers.

Launch an application (choosing from installed on the device) using intent

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.

Automatically revert to previous default SMS app

I have an app that requires temporary access to the device's SMS. In KitKat and above, this access is only granted to the default SMS app, so I'm using:
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
startActivity(intent);
This brings up a dialog asking the user if they let my app become the default SMS app. So far so good. Problem is, once my app completes its operation, I have to ask the user again, if they want to restore their previous app as their default SMS app.
I'd like a way to avoid the second dialog, perhaps by having my app tell the Android OS that it no longer wishes to be the default SMS app, so that the previous app can automatically take over again. I know Android supports this, because if I uninstall my app while it is the default SMS app, Android will revert to the previous one automatically, with no need for user input. Any way to replicate this behaviour of ceding control without uninstalling?
To be eligible to be the default messaging app, your app has to have certain active components registered in the manifest. Disabling any one of them will make your app ineligible, and the system should automatically revert the default. We can use the PackageManager#setComponentEnabledSetting() method to disable a manifest-registered component.
For example, if the Receiver you have registered for the SMS_DELIVER action is named SmsReceiver:
getPackageManager()
.setComponentEnabledSetting(new ComponentName(this, SmsReceiver.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Obviously, before your app could be set as the default again, you would need to re-enable that component, which you can do by calling the above method with PackageManager.COMPONENT_ENABLED_STATE_ENABLED as the second argument.

Can I set the Android "use by default" programmatically for an activity?

I want an activity to launch when a user touches a special URL embedded in an SMS message. I have an activity with an intent filter that matches my unique scheme, host, and pathPrefix. Of course the first time the user touches a link to my trigger url they are presented with the "Complete action using" selection that includes my application and any web browser the user has installed.
Is there a way I can programmatically set the "use by default" setting so my activity is the default action?
(I know this smells like I'd be taking away control from the user so I doubt it is possible, but I figured it wouldn't hurt to ask).
Is there a way I can programmatically set the "use by default" setting so my activity is the default action?
No, sorry.
I know this smells like I'd be taking away control from the user so I doubt it is possible
You have an exquisite olfactory sense... :-)
it is possible as long as your app is signed with platform key, if so, just use ChooserActivity class from com.android.internal.app

Android: How to control the home button

We're trying to provide an application to the mentally and physically handicapped daughter of my neighbor that let's her use an Android tablet as a Talker, i.e., she presses a few big buttons and the devices generates speech. The application is basically a WebView and an additional object in Javascript used to perform and control the speech generation, plus some logic to handle the orientation changes. Html files are generated offline for her specific layout of the talking items. We've also added some music playing and picture viewing facilities to make the device more appealing to her.
Problem is that the home button brings her back into the madness of the Android launcher screen, and that on the test device (Archos 70) the home button is not a physical button but rather shown on the touch screen itself, making it too easy to accidentally hit it.
So I'd like to return to the Android launcher only by pressing a sequence home, back, home with no other action in between.
Can I achieve this by making my application itself the launcher? How can I then get back to the original launcher upon the home, back, home sequence? It seems that this goes deep into the innards of Android, huh?
The only clue I found so far is Overriding Home button for a Car Home replacement app, but this is rated -1 and reported to work in the emulator only. Also I doubt if I could completely abandon the original launcher, as otherwise there is no access anymore to e.g. the USB mass device control to allow new HTML files to be downloaded, the application to be killed and restarted, and so forth.
I'm willing to go for a kludge as well. Maybe a background service could be started that would bring the application to the front again as necessary?
The Home button cannot be overriden. You can write an application that responds to the home intent (that's what a launcher does) but that's all.
Can I achieve this by making my application itself the launcher? How can I then get back to the original launcher upon the home, back, home sequence? It seems that this goes deep into the innards of Android, huh?
Yes. Not too deep into the
innards. You can manually start a launcher by specifying the component, note that this may vary from device to device and user to user, if you're just using this privately you could hard code it, but if you release it you must allow the user to specify their real home app.
/* This should come from a preference that let's the user select an activity that can handle the HOME intent */
String packageName = "com.android.launcher";
String packageClass = "com.android.launcher2.Launcher";
Intent home_intent = new Intent(Intent.ACTION_MAIN);
home_intent.addCategory(Intent.CATEGORY_HOME);
home_intent.setComponent(new ComponentName(packageName, packageClass));
home_intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
/* Here you should catch the exception when the launcher has been uninstalled, and let the user save themselves by opening the Market or an app list or something. Users sometimes use root apps to uninstall the system launcher, so your fake launcher is all that is left. Might as well give the poor user a hand. */
startActivity(home_intent);
Detecting home/back/home is a bit awkard because home won't come as a onKeyEvent but instead as a new intent. Simply long-pressing the back button then display a prompt is probably a safe/good approach.

Categories

Resources