Launch another app

Source: Internet
Author: User

(a) through the package name, class name

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:

[Java]View Plaincopy
    1. Intent Intent = new Intent (Intent.action_main);
    2. Intent.addcategory (Intent.category_launcher);
    3. ComponentName cn = New ComponentName (PackageName, className);
    4. Intent.setcomponent (CN);
    5. 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:

[Java]View Plaincopy
    1. Private void Dostartapplicationwithpackagename (String packagename) {
    2. //Get this app details through the package name, including activities, services, Versioncode, name , and more
    3. PackageInfo packageinfo = null;
    4. try {
    5. PackageInfo = Getpackagemanager (). Getpackageinfo (PackageName, 0);
    6. } catch (Namenotfoundexception e) {
    7. E.printstacktrace ();
    8. }
    9. if (PackageInfo = = null) {
    10. return;
    11. }
    12. //Create a intent of the package name that is a category Category_launcher
    13. Intent resolveintent = new Intent (Intent.action_main, null);
    14. Resolveintent.addcategory (Intent.category_launcher);
    15. Resolveintent.setpackage (Packageinfo.packagename);
    16. //Traverse through the Queryintentactivities method of the Getpackagemanager ()
    17. list<resolveinfo> resolveinfolist = Getpackagemanager ()
    18. . Queryintentactivities (Resolveintent, 0);
    19. ResolveInfo ResolveInfo = Resolveinfolist.iterator (). Next ();
    20. if (resolveinfo! = null) {
    21. //PackageName = parameter Packname
    22. String PackageName = resolveinfo.activityInfo.packageName;
    23. //This is the Launcher activity[form of the app we're looking for: Packagename.mainactivityname]
    24. String className = resolveinfo.activityInfo.name;
    25. //LAUNCHER Intent
    26. Intent Intent = new Intent (Intent.action_main);
    27. Intent.addcategory (Intent.category_launcher);
    28. //Set componentname parameter 1:packagename parameter 2:mainactivity path
    29. ComponentName cn = New ComponentName (PackageName, className);
    30. Intent.setcomponent (CN);
    31. StartActivity (Intent);
    32. }
    33. }

(ii) by action

(c) Transmission parameters: http://www.aboutit.cn/post/195

Launch another app

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.