Android-get apps installed on mobile phones

Source: Internet
Author: User

Android-get apps installed on mobile phones

In the previous blog posts about Android, I introduced some common practical functions in several projects. In this blog, I will introduce you to how to obtain the mobile app installed in the Android mobile phone. All the shoes used by Kingsoft mobile guard or 360 mobile Guard know, these software can obtain the applications installed on the current mobile phone. How are these functions implemented? Let's take a look at how these functions are implemented.

I. Principles

The principle is very simple. We use the PackageManager class provided in Android to obtain information about the apps installed on the mobile phone and encapsulate the information into an object, this object contains information such as the application icon, name, version number, package name, and whether it is a user application. Then, these objects are encapsulated into an object set, then, the set is displayed on the listView interface to form an Application List. The list of apps installed on a mobile phone is displayed to the user.

Is the principle simple? Next, let's implement these functions together.

II. Implementation 1. Create the AppInfo object class of the application

In order to better target and embody the object-oriented encapsulation, I encapsulated every application information I obtained into a java object, this object contains information such as the application icon, name, version number, package name, and whether the application is a user application.

The specific implementation code is as follows:

package cn.lyz.mobilesafe.domain;

import android.graphics.drawable.Drawable;

/ **
 * Obtained application basic information entity class
 * @author liuyazhuang
 *
 * /
public class AppInfo {
// icon
private Drawable app_icon;
//Application Name
private String app_name;
// App version number
private String app_version;
// App package name
private String packagename;
// Whether it is a user app
private boolean isUserApp;
The
The
public AppInfo () {
super ();
// TODO Auto-generated constructor stub
}
public AppInfo (Drawable app_icon, String app_name, String app_version,
String packagename) {
super ();
this.app_icon = app_icon;
this.app_name = app_name;
this.app_version = app_version;
this.packagename = packagename;
}
The
The
public AppInfo (Drawable app_icon, String app_name, String app_version,
String packagename, boolean isUserApp) {
super ();
this.app_icon = app_icon;
this.app_name = app_name;
this.app_version = app_version;
this.packagename = packagename;
this.isUserApp = isUserApp;
}
public Drawable getApp_icon () {
return app_icon;
}
public void setApp_icon (Drawable app_icon) {
this.app_icon = app_icon;
}
public String getApp_name () {
return app_name;
}
public void setApp_name (String app_name) {
this.app_name = app_name;
}
public String getApp_version () {
return app_version;
}
public void setApp_version (String app_version) {
this.app_version = app_version;
}
public String getPackagename () {
return packagename;
}
public void setPackagename (String packagename) {
this.packagename = packagename;
}
The
public boolean isUserApp () {
return isUserApp;
}
public void setUserApp (boolean isUserApp) {
this.isUserApp = isUserApp;
}
The
@Override
public String toString () {
return AppInfo [app_icon = + app_icon +, app_name = + app_name
+, app_version = + app_version +, packagename =
+ packagename +, isUserApp = + isUserApp +];
}
}
 

2. Get the business class AppInfoService of the mobile application
This class mainly implements the main business functions of obtaining the applications installed in the mobile phone, and encapsulates the method of obtaining the applications installed on the mobile phone.

The specific implementation code is as follows:

 

package cn.lyz.mobilesafe.engine;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import cn.lyz.mobilesafe.domain.AppInfo;

/ **
 * Get mobile app
 * @author liuyazhuang
 *
 * /
public class AppInfoService {

private Context context;
private PackageManager pm;
public AppInfoService (Context context) {
// TODO Auto-generated constructor stub
this.context = context;
pm = context.getPackageManager ();
}
The
/ **
* Get all the application information in the phone
* @return
* /
public List getAppInfos () {
// Create collection object to be returned
List appInfos = new ArrayList ();
// Get the collection of all installed apps in the phone
List applicationInfos = pm.getInstalledApplications (PackageManager.GET_UNINSTALLED_PACKAGES);
// Traverse all application collections
for (ApplicationInfo info: applicationInfos) {
The
AppInfo appInfo = new AppInfo ();
The
// Get the icon of the application
Drawable app_icon = info.loadIcon (pm);
appInfo.setApp_icon (app_icon);
The
// Get the name of the application
String app_name = info.loadLabel (pm) .toString ();
appInfo.setApp_name (app_name);
The
// Get the package name of the application
String packageName = info.packageName;
appInfo.setPackagename (packageName);
try {
// Get the version number of the application
PackageInfo packageInfo = pm.getPackageInfo (packageName, 0);
String app_version = packageInfo.versionName;
appInfo.setApp_version (app_version);
} catch (NameNotFoundException e) {
e.printStackTrace ();
}
// Determine whether the application is a user program
boolean isUserApp = filterApp (info);
appInfo.setUserApp (isUserApp);
appInfos.add (appInfo);
}
return appInfos;
}
The
// Determine whether the application is a user program
    public boolean filterApp (ApplicationInfo info) {
    // It turned out to be a system application, users manually upgrade
        if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)! = 0) {
            return true;
            // Apps installed by users
        } else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            return true;
        }
        return false;
    }
}
 

3. Style file styles.xml
Create a new styles.xml file in the res / values directory to define the style information of the application. I mainly define two styles in this file.

The specific code is as follows:

 


 

4. The main layout file applationinstall.xml
The specific implementation code is as follows:

 




    

    

    <framelayout android: layout_height = "fill_parent" android: layout_width = "fill_parent">

        
        

        

            

                

                
            
        
    </ framelayout>

 

5. The layout of each item in the ListView applicationinstall_item.xml
The specific implementation code is as follows

 




    

    
    
    

 

6, custom ListView adapter AppManagerAdapter
This class inherits from BaseAdapter mainly as an adapter for displaying data in List. In this class, the item layout is loaded through the layout loader LayoutInflater. Find the controls on the layout to set the corresponding information.

The specific implementation code is as follows:

 

package cn.lyz.mobilesafe.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.lyz.mobilesafe.R;
import cn.lyz.mobilesafe.domain.AppInfo;

/ **
 * Adapter class managed by App
 * @author liuyazhuang
 *
 * /
public class AppManagerAdapter extends BaseAdapter {

private Context context;
// Layout loader
private LayoutInflater mInflater;
private List appInfos;
The
// Dynamic change appInfos
public void setAppInfos (List appInfos) {
this.appInfos = appInfos;
}

public AppManagerAdapter (Context context, List appInfos) {
this.context = context;
this.appInfos = appInfos;
mInflater = LayoutInflater.from (context);
}
The
public int getCount () {
return appInfos.size ();
}

public Object getItem (int position) {
return appInfos.get (position);
}

public long getItemId (int position) {
return position;
}

public View getView (int position, View convertView, ViewGroup parent) {
// 1 get the control
// 2 get the data
// 3 bind data
View view = null;
if (convertView! = null) {
view = convertView;
} else {
view = mInflater.inflate (R.layout.applationinstall_item, null);
}
The
// Get layout control
ImageView iv_appicon = (ImageView) view.findViewById (R.id.iv_appicon);
TextView tv_appname = (TextView) view.findViewById (R.id.tv_appname);
TextView tv_appversion = (TextView) view.findViewById (R.id.tv_appversion);
The
// Get the AppInfo object at the position
AppInfo appInfo = appInfos.get (position);
The
iv_appicon.setImageDrawable (appInfo.getApp_icon ());
tv_appname.setText (appInfo.getApp_name ());
tv_appversion.setText (appInfo.getApp_version ());
return view;
}

}
 

7. AppManagerActivity
The function implemented by this class is very simple, call methods of other classes, and display the acquired information on the ListView. The specific implementation is to find the controls on the layout in the onCreate method, and call the method in AppInfoService in a thread program to obtain the application installed in the phone, and pass the obtained result to the main thread through the Handler and Message mechanism Display these data on the UI view.

The specific implementation code is as follows:

 

package cn.lyz.mobilesafe.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.lyz.mobilesafe.R;
import cn.lyz.mobilesafe.adapter.AppManagerAdapter;
import cn.lyz.mobilesafe.domain.AppInfo;
import cn.lyz.mobilesafe.engine.AppInfoService;

/ **
 * Manager class managed by APP
 * @author liuyazhuang
 *
 * /
public class AppManagerActivity extends Activity {

protected static final int SUCCESS_GET_APPLICAITON = 0;
The
// Each control in the layout
private RelativeLayout rl_loading;
private ListView lv_appmanage;
private TextView tv_title;
// Package manager
private PackageManager pm;
// Business class for obtaining mobile phone application information
private AppInfoService appInfoService;
// Mobile phone app information collection
private List appInfos;
// User application information collection
private List userAppInfos;
// Whether it is all app programs, the default is true
private boolean isAllApp = true;
// AppManagerAdapter adapter object
private AppManagerAdapter mAdapter;
The
private PopupWindow mPopupWindow;
// mHandler method
private Handler mHandler = new Handler () {
public void handleMessage (android.os.Message msg) {
switch (msg.what) {
case SUCCESS_GET_APPLICAITON:
// Bind data to the listview and hide the loaded controls
mAdapter = new AppManagerAdapter (getApplicationContext (), appInfos);
// Set the data
lv_appmanage.setAdapter (mAdapter);
// Hide RelativeLayout
rl_loading.setVisibility (View.GONE);
//View.VISIBLE (control display) View.INVISIBLE (control hidden but occupy space) View.GONE (control hidden not occupy space)
break;

default:
break;
}
};
};
@Override
protected void onCreate (Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate (savedInstanceState);
setContentView (R.layout.applationinstall);
// Get the controls in the layout
rl_loading = (RelativeLayout) findViewById (R.id.rl_loading);
lv_appmanage = (ListView) findViewById (R.id.lv_appmanage);
tv_title = (TextView) findViewById (R.id.tv_title);
// Instantiate AppInfoService object
appInfoService = new AppInfoService (this);
// Package manager
pm = getPackageManager ();
The
// Get the application information of the mobile phone installed in the sub-thread
new Thread () {
public void run () {
appInfos = appInfoService.getAppInfos ();
The
userAppInfos = new ArrayList ();
for (AppInfo appInfo: appInfos) {
if (appInfo.isUserApp ()) {
userAppInfos.add (appInfo);
}
}
Message msg = new Message ();
msg.what = SUCCESS_GET_APPLICAITON;
mHandler.sendMessage (msg);
};
} .start ();
The
}
}
 

3. Operation effect
Loading application

Get the applications installed in the phone

4. Warm tips
In this example, for the sake of aspect, I have written some text directly in the layout file and related classes. In real projects, you should write these texts in the strings.xml file, reference these resources externally, and remember, This is the most basic development knowledge and specification as an Android programmer. I just wrote it directly in the class and layout files for convenience.

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.