How do I launch com.android.launcher? - android

I created an app that acts as a launcher, which works as expected, but I'd like to give the user the ability to launch the native Android Launcher.
I know that the package name for the Android Launcher is com.android.launcher
However, when I try to get the Launch intent name from this package by calling
packageManager.getLaunchIntentForPackage("com.android.launcher");
that comes up null.
So, I'm at a loss as to how to use the package name to launch it, and am wondering if perhaps there's some alternate way?
TIA

I know that the package name for the Android Launcher is com.android.launcher
Except that may or may not be on any given device. In fact, I would expect it to be on maybe a few percent of devices. Most manufacturers replace the stock home screen with their own.
am wondering if perhaps there's some alternate way?
Use PackageManager and queryIntentActivities() to find all activities that support ACTION_MAIN and CATEGORY_HOME. Remove your activity from the list. If there is only one left, launch that activity. If there is more than one left, whip up your own chooser to show the available options. That way, no matter what other home screen(s) exists, you will be able to launch it.
UPDATE
Given a ResolveInfo named launchable, and an ACTION_MAIN/CATEGORY_HOME template Intent named i, to launch that activity, do:
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);

Related

Android - can't go back to mainActivity after starting a different app

Okay, I think I'm missing something here but can't seem to find a way around it :\
So this is my scenario, I have two apps, A and B. A Opens a B with the following intent:
PackageManager pm = getPackageManager();
Intent n = pm.getLaunchIntentForPackage(currAppInfo.getName());
n.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(n);
(currAppInfo is a custom object and getName returns the package name.)
Anyway, B is installing APKs. A receives the package installed broadcast and should be now moved back to front, how ever if I'm starting app A with an intent
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
Instead of seeing A's mainActivity screen all I see is B's main activity screen.
Why is that? Is it the way I open the apps with the intents or am I missing something more basic here?

How to create a shortcut of an app on a SPECIFIC launcher app?

Background
I already know how to put a shortcut to an app globally:
Intent shortcutIntent=new Intent();
shortcutIntent.setComponent(new ComponentName(packageName,fullPathToActivity));
final Intent putShortCutIntent=new Intent();
putShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
//... <=preparing putShortcutIntent with some customizations (title, icon,...)
putShortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(putShortcutIntent);
This works well.
What I do wish to do is to be able to put the shortcut only to a specific launcher app (in fact it's the default launcher app, but that's not the problem).
The problem
For some reason, instead of putting the shortcut on the launcher app i've chosen (in this case "nova launcher") , I end up getting the shortcut created on the touchwiz launcher alone.
What I've tried
the obvious solutions were:
putShortcutIntent.setClassName(launcherPackageName,broadcastReceiverClassName);
or
putShortcutIntent.setPackage(launcherPackageName);
none worked, even though i've seen the values fine during debug mode (the parameters were legit).
The weird thing is that if I use this, it puts the shortcut only on the touchwiz launcher...
The question
Why does it occur?
How come I can't choose where to send the broadcast to?
is there a better way to achieve this?
EDIT: here's how I get the default broadcastReceiver that can handle the shortcut creation intent:
final ResolveInfo defaultActivityForLauncherIntent=... //here i return the correct app that is the default launcher
Intent intentForAddingShortcutOnDesktop=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//<= here I prepare the intent of creating a shortcut
final List<ResolveInfo> broadcastReceiversResolveInfos=context.getPackageManager().queryBroadcastReceivers(intentForAddingShortcutOnDesktop,0);
for(final ResolveInfo resolveInfo : broadcastReceiversResolveInfos)
if(resolveInfo.activityInfo.packageName.equals(defaultActivityForLauncherIntent.activityInfo.packageName))
{
intentForAddingShortcutOnDesktop.setComponent(new ComponentName(//
resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name));
break;
}
context.sendBroadcast(intentForAddingShortcutOnDesktop);
it seems part of the confusion was that the play store was set to create new shortcuts.
This, and the fact i forgot to query the broadcastReceivers instead of activities...

How to change main activity in Android programmatically

Is it possible to create two entry points to an application in Android, I mean can I switch the main activity programmatically?
Every exported activity is a potential entry point into your app; a foreign app can start any of them with an intent. (An intent-filter comes with an implicit android:export.) You can however only have one entry point that the launcher will respect. To simulate a second launch-point, either
Provide a completely separate app with the purpose of starting one of your exported activities, or
Give your 'launch' activity the sole purpose of immediately starting one or another activity based on some logic (a saved preference, a phase-of-moon calculation, anything).
check this one below
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
I think you are talking about launching activity decision based on some events, then you need to add a broadcast receiver, like by clicking on app icon on launcher if you want to start Activity1. then add intent filters to this activity Action_MAIN and ACTION_LAUNCHER, if you want to start Activity2 on phone boot up, then add filter to this activity, BOOT_COMPLETED.
If you are talking about to launch other apps from your apps then this can be the code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);

Open my application from another in android

My boss asked me to prove that my application behaves properly when summoned by another application (dunno why he asked that).
So I have two apps here, one launches a second one. How I launch the specific app I want? Using Intent launch seemly any generic app that reaches a certain goal, not the app I really want.
Give this a try.
Intent secondIntent = new Intent();
secondIntent.setAction(Intent.ACTION_MAIN);
secondIntent.setClassName("com.example", "com.example.YourSecondApp");
startActivity(secondIntent);
I should point out that com.example should be the package of your second application (the one you want to call) and com.example.YourSecondapp is the class name where you have your onCreate() method.
Intent secondApp = new Intent("com.test.SecondApp");
startActivity(secondApp);
Check out for more examples
http://developer.android.com/resources/faq/commontasks.html#opennewscreen
Create one Intent using the following code
Explicit Intent
When you know the particular component(activity/service) to be loaded
Intent intent = new Intent();
intent.setClass("className/package name");
start<Activity/Service>(intent);
Imlicit Intent
When we do not have the idea which class to load and we know the Action to be perform by the launched application we can go with this intent.
Action needs to set, and the Android run time fallows the intent Resolution technique and list out(one or more components) the components to perform the action. from the list out components (if more than one), user will get the chance to launch his chosen application

How would I go about getting the package names of the applications that can be Home on android

My activity gets set as the default Home by the user, but I want to be able to launch the default Home from within my activity as well. I've tried hard coding the following:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
PackageManager pm = getPackageManager();
String packageName = "com.android.launcher";
String className = "com.android.launcher.Launcher";
final ComponentName cn = new ComponentName(packageName, >className);
intent.setComponent(cn);
This seems to work on my droid, but force closes on the HTC Ally. I'm thinking there's an easier way to just get a list of the apps that have the category Home and Default.
For those that have used the Home Swittcher app. I essentially want to generate that list of installed default Home activities on the phone.
Step #1: Create an Intent that launches whatever the user has for the default HOME application. In other words, do not try putting in the ComponentName.
Step #2: Call queryIntentActivities() on PackageManager to find out who all can respond to that Intent.
Step #3: If there are only two entries in the list, find out which is yours, and the other one is the platform's. If there are 3+ entries in the list, remove yours and let the user choose among the remaining options.
You could sorta combine #2 and #3 by using queryIntentActivityOptions() if you like, setting it up to filter your own Intent out of the list.

Categories

Resources