Android Check if the phone has a way to install an application _android

Source: Internet
Author: User
Tags sqlite database

This article is an example of Android checking that the phone has a way to install an application. Share to everyone for your reference, specific as follows:

Mobile phone users are particularly tired of manual input of a large number of information, in a program can invoke other programs or functions can often give users a better user experience, so that users feel more cordial, so the integration between the program can lead to multi-party software work together to "win" situation.

Then we in our own program to invoke other programs are often to check the phone has not installed the software and then do the appropriate operation, such as not installed, with intent users to boot to the Android Market to download the program, such as installed then open the program, and incoming parameters and call its function.

Here's how to tell if a phone has a program installed:

Private Boolean isavilible (context context, String PackageName) {
     final Packagemanager Packagemanager = Context.getpackagemanager ()//Get Packagemanager
     list< packageinfo> pinfo = Packagemanager.getinstalledpackages (0);//Get package information for all installed programs
     list<string> pname = new Arraylist<string> ( ///To store the package name for all installed programs
     //Remove the package name from pinfo, pressing into PName list
       if (pinfo!= null) {for
       (int i = 0; i < pinfo.size (); i+ +) {
         String pn = pinfo.get (i) PackageName;
         Pname.add (PN);
       }
     Return Pname.contains (PackageName);//Determine if there is a package name for the target program in the PName, true, no false
}

The following is the logic that calls the method for judgment:

installed, open program, pass in Parameter package name: "Com.skype.android.verizon"
if (isavilible (this, "Com.skype.android.verizon")) {
    Intent i = new Intent ();
    ComponentName cn = New ComponentName ("Com.skype.android.verizon",
        "com.skype.android.verizon.SkypeActivity");
    I.setcomponent (CN);
    Startactivityforresult (i, RESULT_OK);
}
Not installed, jump to market download this program
else {
    uri uri = uri.parse ("Market://details?id=com.skype.android.verizon");//id for Package name
    Intent it = new Intent (Intent.action_view, URI);
    StartActivity (it);
}

The above method is written by myself, another online check the following method, it is said to be able to, but I used a bit of a problem, may be my incoming parameters have problems, the following two functions to be introduced to the action and intent, we can try.

Method One:

public static Boolean isintentavailable (context context, String action) {
    final Packagemanager Packagemanager = Conte Xt.getpackagemanager ();
    Final Intent Intent = new Intent (action);
    list<resolveinfo> list =
        packagemanager.queryintentactivities (Intent,
            Packagemanager.match_default _only);
    return list.size () > 0;
}

Method Two:

public boolean isintentavailable (Intent Intent) {
    final Packagemanager Packagemanager = This.getpackagemanager ();
    list<resolveinfo> List = packagemanager.queryintentactivities (Intent,
        Packagemanager.match_ default_only);
    return list.size () > 0;
}

Then do the appropriate operation, if not installed, using intent to boot the user to the Android Market to download the program, such as installed then open the program, and pass parameters and call its function.

Here's how to tell if a phone has a program installed:

Private Boolean isavilible (context context, String PackageName) {
 final Packagemanager Packagemanager = Context.getpackagemanager ()//Get Packagemanager
 list< packageinfo> pinfo = Packagemanager.getinstalledpackages (0);//Get package information for all installed programs
 list<string> pname = new Arraylist<string> ( ///To store the package name for all installed programs
 //Remove the package name from pinfo, pressing into PName list
   if (pinfo!= null) {for
   (int i = 0; i < pinfo.size (); i+ +) {
     String pn = pinfo.get (i) PackageName;
     Pname.add (PN);
   }
 Return Pname.contains (PackageName);//Determine if there is a package name for the target program in the PName, true, no false
}

The following is the logic that calls the method for judgment:

installed, open program, pass in Parameter package name: "Com.skype.android.verizon"
if (isavilible (this, "Com.skype.android.verizon")) {
    Intent i = new Intent ();
    ComponentName cn = New ComponentName ("Com.skype.android.verizon",
        "com.skype.android.verizon.SkypeActivity");
    I.setcomponent (CN);
    Startactivityforresult (i, RESULT_OK);
}
Not installed, jump to market download this program
else {
    uri uri = uri.parse ("Market://details?id=com.skype.android.verizon");//id for Package name
    Intent it = new Intent (Intent.action_view, URI);
    StartActivity (it);
}

The above method is written by myself, another online check the following method, it is said to be able to, but I used a bit of a problem, may be my incoming parameters have problems, the following two functions to be introduced to the action and intent, we can try.

Method One:

public static Boolean isintentavailable (context context, String action) {
    final Packagemanager Packagemanager = Conte Xt.getpackagemanager ();
    Final Intent Intent = new Intent (action);
    list<resolveinfo> list =
        packagemanager.queryintentactivities (Intent,
            Packagemanager.match_default _only);
    return list.size () > 0;
}

Method Two:

public boolean isintentavailable (Intent Intent) {
    final Packagemanager Packagemanager = This.getpackagemanager ();
    list<resolveinfo> List = packagemanager.queryintentactivities (Intent,
        Packagemanager.match_ default_only);
    return list.size () > 0;
}

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

Related Article

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.