Several ways to launch other apps in Android

Source: Internet
Author: User

1, know the name of the package to start and the activity to start:

Intent Intent = new Intent ();

/** the parameters of the following methods refer to the package name to start the application and the corresponding activity*/
ComponentName cpn= New ComponentName ("Com.ldm.demo", "com.ldm.demo.TextActivity");
Intent. SetComponent (CPN);
StartActivity (Intent);

2, only know the package name of the app:

Packagemanager Packagemanager = Getpackagemanager ();
Intent intent=new Intent ();
Intent =packagemanager.getlaunchintentforpackage ("PackageName");/**packagename refers to the package name to start the application */
if (intent==null) {
System.out.println ("APP not found!");
}
StartActivity (Intent);
}
}

3, know the package name to start and the activity app to start to start and pass the data:

  1. ComponentName componentname = new ComponentName ("Com.ldm.demo", "com.ldm.demo.TestActivity");
  2. Intent Intent = new Intent ();
  3. Bundle bundle = new bundle ();
  4. Bundle.putstring ("Data", data);
  5. Intent.putextras (bundle);
  6. Intent.setcomponent (componentname);
  7. StartActivity (Intent);
  8. 4, the case of unknown package name:
  9. /** Gets the package name of the app in the phone and returns a list collection **/
    Public list<packageinfo> Getallapps () {
    list<packageinfo> apps = new arraylist<packageinfo> ();
    Packagemanager Packagemanager = This.getpackagemanager ();
    /** get all apps in your phone * /
    list<packageinfo> paklist = pmanager.getinstalledpackages (0);
    for (int i = 0; i < paklist.size (); i++) {
    PackageInfo Pak = (packageinfo) paklist.get (i);
    /** Determine if a non-system preinstalled application (greater than 0 is pre-installed for the system, less than or equal to 0 for non-system applications) */
    if ((Pak.applicationInfo.flags & Pak.applicationInfo.FLAG_SYSTEM) <= 0) {
    Apps.add (Pak);
                    }     
                }     
    return apps;
        }   
     Once you have the package name, you can start the app by getting the package name that you want to start:
    Public void Launchapp () {
    Packagemanager Packagemanager = This.getpackagemanager ();
    list<packageinfo> packages = Getallapps ();
    PackageInfo pa = null;
    for (int i=0;i<packages.size (); i++) {
    pa = packages.get (i);
    /** Get the application name * /
    String Applabel = Packagemanager.getapplicationlabel (pa.applicationinfo). toString ();
    /** Get Package name * /
    String apppackage = pa.packagename;
    log.d ("+i, applabel+" "+apppackage");
                }   
    Intent Intent = packagemanager.getlaunchintentforpackage ("Jp.co.johospace.jorte");
    startactivity (Intent);
            }   


Several ways to launch other apps in Android

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.