I am running into a very strange situation whereby I am getting a list of all the installed packages that have a CATEGORY_HOME intent.
My intention is to manually launch the native Home application (which is currently NOT the Default native Home application, because my application has that role).
So, the method I'm using (beneath) correctly identifies that there are two apps that are set up as CATEGORY_HOME.
When I try to launch the one that is mine (fetching the Launcher activity) it works fine. However, when I try to fetch the Launcher activity for the default one, it comes back as null.
So... I'm stumped. How do I determine what I should actually be launching when the package name of the stock home app returns null when I try and pull the relevant Launch activity via getLaunchIntentForPackag from it?
Here's what I'm doing (with some comments to avoid confusion), and for the record I know that not all of them will have the expression "android" in the name space, but I'm trying to get this to work initially on a device that does come back with that string so that part of it isn't the issue.
//get a list of all apps that set themselves up as CATEGORY_HOME
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
final List<ResolveInfo> list = ((PackageManager)getPackageManager()).queryIntentActivities(intent, 0);
String packageName = null;
//look for the one that has the word android in the package name
for(ResolveInfo ri : list){
if(ri.activityInfo.packageName.indexOf("android") != -1)
//this does get set correctly and looks like "com.sec.android.app.launcher"
packageName = ri.activityInfo.packageName;
}
PackageManager pm = SlidePlayer.this
.getPackageManager();
Intent it = pm.getLaunchIntentForPackage(packageName);
//it is NULL so this doesn't work
startActivity(it);
***EDIT
Trying out the following methodology based on CommonsWare's advice...
String packageName = null;
String className = null;
for(ResolveInfo ri : list){
//L.d("HOME PACK = " + ri.);
if(ri.activityInfo.packageName.indexOf("android") != -1){
className = ri.activityInfo.applicationInfo.className;
packageName = ri.activityInfo.applicationInfo.packageName;
}
}
//PackageManager pm = SlidePlayer.this
//.getPackageManager();
Intent it = new Intent();//pm.getLaunchIntentForPackage(packageName);
//both packageName and className appear to be set correctly
//packageName = "com.sec.android.app.launcher"
//className = "com.android.launcher2.LauncherApplication"
it.setClassName(packageName, className);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
EDIT 2*
semi huzzah...
On one device (Samsung Note II) the key is to set it via Component like so...
Intent it = new Intent();//pm.getLaunchIntentForPackage(packageName);
ComponentName cn = new ComponentName(packageName, className);
it.setComponent(cn);
//it.setClassName(packageName, className);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
This doesn't work on the Samsung Tab 10" or the Motorola M (which are the only other 2 devices I have thus far tested with this methodology).
getLaucnIntentForPackage looks specifically for CATEGORY_INFO or CATEGORY_LAUNCHER - not CATEGORY_HOME, in which case it will return null. From the Documentation:
public abstract Intent getLaunchIntentForPackage (String packageName)
Added in API level 3 Return a "good" intent to launch a front-door
activity in a package, for use for example to implement an "open"
button when browsing through packages. The current implementation will
look first for a main activity in the category CATEGORY_INFO, next for
a main activity in the category CATEGORY_LAUNCHER, or return null if
neither are found.
However, when I try to fetch the Launcher activity for the default one, it comes back as null.
That's because most firmware home screens are not designed to be launched by home screens, and so probably do not include a launcher activity.
How do I determine what I should actually be launching when the package name of the stock home app returns null when I try and pull the relevant Launch activity via getLaunchIntentForPackag from it?
Since it is a home screen, you know that it must have an activity with the same ACTION_MAIN/CATEGORY_HOME as you do. Find that one and start it.
Related
I am trying to open the Google Voice Search Application
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.voicesearch");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
This does not launch the voice search application,
however if I use com.google.android.apps.maps as the package name then the Google Maps app is opened.
I don't understand why Voice Search is not opening, even though the package name is correct.
Solution
Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
startActivity(intent);
Please see
Launch Preinstalled App from activity (Google Voice Search) Android for more information on the solution.
Thank you.
As you can read here, getLaunchIntentForPackage (String packageName)
Return a "good" intent to launch a front-door activity in a package [...]
The current implementation will look first
for a main activity in the category CATEGORY_INFO, next for a main
activity in the category CATEGORY_LAUNCHER, or return null if neither
are found.
so, in the intent, the category will already be set. If you manually change it, probably you are breaking the intent, since the category could not be the correct one that the manager found.
so, just remove the line
i.addCategory(Intent.CATEGORY_LAUNCHER);
Hello i wan't to keep home activity name for launch it from my app.
I know we can launch the launcher without know is package name but.
I'm making a custom home and i will set it as default so this will launch my custom home.
I wan't to keep the launcher activity name on a shared preference the first time i launch my custom home and then i will be able to go on the default home from my personal home(without delete my home preference i will keep it as default and launch default home just for some debug test)
Don't know if i'm clear; i have difficult to understood myself in this subject so for resume
I need at the first launch of my activity get the name of the default launcher(because it's not com.android.launcher for every device) and keep it to be able to launch it after some time
Any idea?
Finaly i don't keep the name i don't care of it i just use the following code to launch a launcher who is not mine :P
PackageManager pm = getPackageManager();
Intent i = new Intent("android.intent.action.MAIN");
i.addCategory("android.intent.category.HOME");
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
if (lst != null)
{
for (ResolveInfo resolveInfo : lst) {
if (resolveInfo.activityInfo.packageName != getPackageName()){
Intent res = new Intent();
String mPackage = resolveInfo.activityInfo.packageName;
String mClass = resolveInfo.activityInfo.name;
res.setComponent(new ComponentName(mPackage,mClass));
startActivity(res);
}
}
}
you can get application registered with intent action android.intent.category.HOME, and you can get package list for aplication registered with this intent.
May be this can also help
I want to get default home application name.For this i used
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
intent.addCategory("android.intent.category.DEFAULT");
ResolveInfo resolveinfo =getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
defaultHomeName = resolveinfo.activityInfo.name;
This returns com.android.internal.app.ResolverActivity and when I tried following
List<RunningTaskInfo> runningTasks =((ActivityManager) getApplicationContext().getSystemService("activity")).getRunningTasks(1);
if (runningTasks != null && !runningTasks.isEmpty()) {
for (int i = 0; i < runningTasks.size(); i++) {
RunningTaskInfo runningtaskinfo = (RunningTaskInfo) runningTasks.get(i);
}
}
got home name as com.sec.android.app.twlauncher.Launcher inside this when on the home screen .
Why the same application shows different names?How to get a unique name for default home application for all devices?
Why the same application shows different names?
Because they are not the same application.
Specifically, com.android.internal.app.ResolverActivity is the resolver activity, what we tend to refer to as the activity chooser, or simply "chooser" for short. That comes up for resolveActivity() because there are two or more activities that can handle your chosen Intent.
How to get a unique name for default home application for all devices?
By using queryIntentActivities() on PackageManager, instead of resolveActivity(), you will get a list of all installed home screens. If this list has more than one entry on it, after filtering out your own home screen (should you be writing one), you have no reliable way necessarily of determining which of those is the "default home application".
hi Its been 2 days looking for this simple problem. I want to launch Android own launcher from my application EVEN if its not set as default.
final PackageManager packageManager=getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage("com.android.launcher");
this return null for Android own launcher but if I try custom launcher is give me successfully
Found the solution, after investigating source code of getLaunchIntentForPackage. As per documentation,
The current implementation will look first for a main activity in the
category CATEGORY_INFO, next for a main activity in the category
CATEGORY_LAUNCHER, or return null if neither are found.
So, the function don't look for CATEGORY_HOME, i rewrote it in following way, it worked perfectly.
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_HOME);
intentToResolve.setPackage("com.android.launcher");
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null)
{
Intent intent = new Intent(intentToResolve);
intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
Are you sure the default Google Android Launcher being installed on your device? If not, then it's truly NULL.
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.