"Android" detects whether the app is installed, installed, and opened
Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Check if the app is installed</p></p><p><p>There are many ways to detect whether an app is installed, and here are a few examples. The basic use of Packagemanager to Complete.</p></p><p><p>a, Obtain the installed package by packagemanager, and then compare whether it is Installed. Code:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public boolean isappinstalled (context context, String packagename) { final Packagemanager Packagemanager = Context.getpackagemanager (); list<packageinfo> pinfo = packagemanager.getinstalledpackages (0); list<string> pName = new arraylist<string> (); If (pinfo! = Null) {for (int i = 0; i < pinfo.size (); i++) { String pn = pinfo.get (i). packagename; Pname.add (pn); } } return Pname.contains (packagename); } </pre></pre><br>b, to determine if there is an installation by obtaining the Activity's intent<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">/** * Determine if an app is installed * @param context * @param activitypath need to open the first page of the App's activity full path * @return */public static Boolean Appisin Stalled (Context context,string Activitypath) {final packagemanager packagemanager = Context.getpackagemanager (); list<resolveinfo> ResolveInfo = packagemanager.queryintentactivities (new Intent (activitypath), packagemanager.get_activities); if (resolveinfo!=null &&!resolveinfo.isempty ()) {return true;} Return false;}</pre></pre><br>There are also getpackageinfo methods that pass packagemanager, but this method triggers an exception that is not recommended for Use.<p><p></p></p><p><p><br></p></p><p><p>2, Install App,app There are two ways to get, the first is placed in the raw or Asste folder and APK package together, the other is to download the Server. This is unclear.</p></p><p><p>The APK installation app has a normal installation and a silent installation (requires root access), which is described below for normal mode Installation. On the Code.</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">/** * Install an app * @param context * @param filePath need to install the file path * @return */public static void install (context context, String FilePath) {Intent i = new Intent (intent.action_view); i.setdataandtype (uri.parse ("file://" + filePath), "application/ Vnd.android.package-archive "); context.startactivity (i);}</pre></pre><br><br><p><p></p></p><p><p>3. Launch the installed app.</p></p><p><p>This is too simple. Call Intent directly with the full-package name, such as Startactivites (new Intent ("com.fcly.test.mainActivity"));</p></p><p><p><br></p></p><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>"Android" detects whether the app is installed, installed, and opened</p></p></span>
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