Android launches another app in one app by package name or class name

Source: Internet
Author: User

Development sometimes requires that another application be launched in one application, such as a list of installed programs that launcher loads, and another app can be launched when the icon is clicked. In general, we know that another app's package name and mainactivity name can be started directly from the following code:

Intent Intent = new Intent (intent.action_main);
Intent.addcategory (Intent.category_launcher);
ComponentName cn = New ComponentName (PackageName, className);
Intent.setcomponent (CN);
StartActivity (Intent);

But more often, we do not know the target application's Startup activity class name, but only know the package name, then we need to do this, I encapsulated into a method, as follows:

private void Dostartapplicationwithpackagename (String packagename) {

Get this app details through the package name, including activities, services, Versioncode, name, and more
PackageInfo packageinfo = null;
try {
PackageInfo = Getpackagemanager (). Getpackageinfo (PackageName, 0);
} catch (Namenotfoundexception e) {
E.printstacktrace ();
}
if (PackageInfo = = null) {
Return
}

Create a intent of the package name that is a category Category_launcher
Intent resolveintent = new Intent (intent.action_main, NULL);
Resolveintent.addcategory (Intent.category_launcher);
Resolveintent.setpackage (Packageinfo.packagename);

Traverse through the Queryintentactivities method of the Getpackagemanager ()
list<resolveinfo> resolveinfolist = Getpackagemanager ()
. Queryintentactivities (resolveintent, 0);

ResolveInfo ResolveInfo = Resolveinfolist.iterator (). Next ();
if (resolveinfo! = null) {
PackageName = parameter Packname
String PackageName = resolveinfo.activityInfo.packageName;
This is the launcher of the app we're looking for. Activity[Organization: Packagename.mainactivityname]
String className = resolveinfo.activityInfo.name;
LAUNCHER Intent
Intent Intent = new Intent (intent.action_main);
Intent.addcategory (Intent.category_launcher);

Set componentname parameter 1:packagename parameter 2:mainactivity path
ComponentName cn = New ComponentName (PackageName, className);

Intent.setcomponent (CN);
StartActivity (Intent);
}
}

Android launches another app in one app by package name or class name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.