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.
Related
Basically I'd like to start the current phone call ui from a button in one of my activities.
So far, I've been able to start a new call using
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:000000"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
or to move to the phonebook using:
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I've also tried to use that code, but it seems not to be working on HTC devices and some Huawei (ri is null).
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage("com.android.phone");
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_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
What I'd like to get is move directly to the call ui. The behaviour is the same as in this procedure:
Be in a phone call
Go back to the Home screen
Expand the notification bar
Click on the call row
I don't know how to achievve and how to make it works on all devices.
Thanks for your precious help.
you have to do as suggested on this answer: android.permission.CALL_PHONE for tablets
add the call phone permission:
<uses-permission android:name="android.permission.CALL_PHONE"/>
and then call with Intent.ACTION_CALL.
Intent intentcall = new Intent(
Intent.ACTION_CALL,
Uri.parse("tel:" + phonenumber));
startActivity(intentcall);
edit:
there's a way you could do, but it won't work in Lollipop and forward, as it's been deprecated due to security reasons.
You could use the ActivityManager to get the running tasks and then analyse the content of each RunningTaskInfo to determine which one is the "phone" and then use the task ID to call moveTaskToFront. It possibly work, but as I said, it will not work with Lollipop and future versions.
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);
I use the following code snippet to launch an app on device:
Context mContext = getContext();
String packageName = getPackageName(); //the app to launch
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
It works, the app get launched, however, I don't figure out a way to close the launched app by using the packageName.
How to close the launched app if I only know the package name?
You cannot close another app. Only the system can do that.
But if you are also the author of that other app, you could create a receiver in that app's activities that accepts an intent that tells it to finish() the activities.
You cannot close other application from your app.your problem has a workaround,after starting app based on package name you may send home intent which will give same experience to user.
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
//sleep for 250ms or whatever time using handler
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
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.
I am new to Android. Say, I open an app and I would like to open another App after clicking a button. How can I accomplish this task? Would appreciate if you can provide me some tutorial on this.
Intent intent = new Intent();
intent.setClassName("**package.name**", "**package.name.LauncherActivityName**");
startActivityForResult(intent,REQUEST_CODE);
You need to know the package and class names of the activity to call
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("app package name");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Use this code:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.package");
startActivity(launchIntent);
The app you want to launch must be on the device.
Intent appIntent = getPackageManager().getLaunchIntentForPackage("your app package name ");
startActivity(appIntent );
If the other application is a pre-packaged application mean, this tutorial may help you.
If the other application is going to be your application, then you need to learn Implicit Intent tutorials.
Also include the activity of the other application which you are planning to call in the Manifest file of the calling package also.