Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47114331
Some of the previous blog post about Android. We have introduced some useful functions that are often used in several projects. Well, in this blog post. I will show you how to get the Android phone installed in the mobile app, the use of Jinshan mobile phone defender or 360 mobile phone defender's children's shoes are known. These software are able to obtain the applications installed in the current phone.
So, how do these functions be implemented? Let's take a look at how these features are implemented in detail.
First, the principle
The principle is very easy, we use the Packagemanager class provided in Android, to obtain the application information installed in the phone, to encapsulate this information into an object, including the application's icon, name, version, package name, whether it is a user application and other information. These objects are then encapsulated into a collection of objects. The collection is then displayed on the ListView of the interface. form an application list. Presented to the user is a list of applications installed in the phone.
Is the principle not very easy? Below, let's implement these features together.
Ii. implement 1, create an application entity class AppInfo
To be more object-oriented and to embody object-oriented encapsulation. Each application information I get is encapsulated into a Java object that includes the application's icon, name, version, package name, and information such as the user's application.
Detailed implementation code such as the following:
Package Cn.lyz.mobilesafe.domain;import android.graphics.drawable.drawable;/** * Get app Basic information entity class * @author Liuyazhuang * * /public class AppInfo {//icon private drawable app_icon;//app name private string app_name;//app version private string app_version;// App Package name private String packagename;//is User appprivate Boolean isuserapp;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;} 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;} public Boolean Isuserapp () {return isuserapp;} public void Setuserapp (Boolean isuserapp) {This.isuserapp = Isuserapp;} @Overridepublic String toString () {return "AppInfo [app_icon=" + App_icon + ", app_name=" + app_name+ ", app_version=" + A Pp_version + ", packagename=" + PackageName + ", isuserapp=" + Isuserapp + "]";}}
2. Get the business class of the mobile app Appinfoservice
This class primarily implements the main business functions of acquiring applications installed in the phone. Encapsulates how to get a phone-installed application.
Detailed implementation code such as the following:
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 stubthis.context = context;pm = Context.getpackagemanager ();} /** * Get all the app info on the phone * @return */public list<appinfo> Getappinfos () {//Create collection object to return list<appinfo> Appinfos = new Arraylist<appinfo> ();//Get all installed app collections on your phone list<applicationinfo> Applicationinfos = Pm.getinstalledapplications (packagemanager.get_uninstalled_packages);//Traverse all application sets for (ApplicationInfo info: Applicationinfos) {AppInfo AppInfo = new AppInfo();//Gets the application's icon drawable app_icon = Info.loadicon (PM); Appinfo.setapp_icon (App_icon);//Gets the name of the app string app_name = Info.loadlabel (PM). toString (); Appinfo.setapp_name (app_name);//Gets the package name of the app string packagename = Info.packagename; Appinfo.setpackagename (PackageName); try {//Get the version of the app PackageInfo PackageInfo = pm.getpackageinfo (packagename, 0); String app_version = packageinfo.versionname;appinfo.setapp_version (app_version);} catch (Namenotfoundexception e) {e.printstacktrace ();} Infer whether the application is a user program, Boolean isuserapp = Filterapp (info), Appinfo.setuserapp (Isuserapp); Appinfos.add (appInfo);} return Appinfos;} Infer whether the application is a user program public boolean filterapp (ApplicationInfo info) {//original is a system application, the user manually upgrades if (Info.flags & appli Cationinfo.flag_updated_system_app)! = 0) {return true; User-installed applications} else if ((Info.flags & applicationinfo.flag_system) = = 0) {return true; } return false; }}
3. style file Styles.xml
Create a new Styles.xml file under the Res/values folder to define the style information for your application. I have defined two styles in this file primarily.
Detailed code such as the following:
<style name= "View_divide_line_style" > <item name= "Android:layout_width" >fill_parent</item> <item name= "android:layout_height" >1dip</item> <item name= "Android:layout_margintop" > 5dip</item> <item name= "Android:background" > @drawable/devide_line</item> </style >
<style name= "Text_title_style" > <item name= "Android:layout_width" >fill_parent</item> <item name= "Android:layout_height" >wrap_content</item> <item name= "Android:gravity" >center </item> <item name= "Android:textcolor" > #42E700 </item> <item name= "Android:textsize" >25sp</item> </style>
4. Main layout file Applationinstall.xml
Detailed implementation code such as the following:
<?XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android "Android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "> & Lt TextView android:id= "@+id/tv_title" style= "@style/text_title_style" android:text= "All Programs"/> <v Iew style= "@style/view_divide_line_style"/> <framelayout android:layout_width= "Fill_parent" Androi d:layout_height= "Fill_parent" > <!--android:cachecolorhint= "#00000000" cache color default is yellow Android:divi Der= "#00ffffff" cutting line android:dividerheight= "3.0dip" cutting line width--<listview android:id= "@ +id/lv_appmanage "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " Android:cachecolorhint= "#00000000" android:divider= "#fff" android:fastscrollenabled= "true" Android:dividerheight= "1.0diP "android:paddingleft=" 3.0dip "android:paddingright=" 3.0dip "/> <relativelayout Android:id= "@+id/rl_loading" android:layout_width= "fill_parent" android:layout_height= "Fill_par Ent "> <linearlayout android:layout_width=" wrap_content "Android:layout_hei ght= "wrap_content" android:orientation= "vertical" android:layout_centerinparent= "true" > <progressbar android:id= "@+id/pb" android:layout_width= "Wrap_conte NT "android:layout_height=" wrap_content "android:layout_gravity=" center " /> <textview android:layout_width= "Wrap_content" Androi d:layout_height= "Wrap_content" android:layout_margintop= "10dip" android:text= "Loading application Program.. " Android:textcolor= "#fff" android:textsize= "22sp"/> </LinearLayout> </re Lativelayout> </FrameLayout></LinearLayout>
5, each item in the ListView layout Applationinstall_item.xml
Detailed implementation code such as the following
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "android:orientation=" horizontal "android:paddingbottom=" 8.0dip "a ndroid:paddingleft= "6.0dip" android:paddingright= "5.0dip" android:paddingtop= "8.0dip" > <imageview A Ndroid:id= "@+id/iv_appicon" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_centervertical= "true" android:src= "@drawable/ic_launcher"/> <textview android:id= "@ +id/tv_appname "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layo Ut_centervertical= "true" android:layout_marginleft= "4.0dip" android:layout_torightof= "@id/iv_appicon" Android:textcolor= "#fff" android:text= "I most Sway" android:textsize= "16.0dip"/> <textview an Droid:id= "@+id/tv_appversion" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "True" android:layout_centervertical= "true" android:layout_marginleft= "4.0dip" android:textcolor= "#ff F "android:layout_marginright=" 10DP "android:text=" 1.0 "android:textsize=" 16.0dip "/></relative Layout>
6. Self-defined listview adapter Appmanageradapter
This class inherits from Baseadapter primarily as the adapter for the list display data, where the layout of the entries is loaded by the layout loader layoutinflater. Locate the control on the layout to set the corresponding information.
Detailed implementation code such as the following:
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;/** * App managed Adapter class * @author Liuyazhuang * */public class Appmanageradapter extends Baseadapter {private Context context;//layout loader private layoutinflater minflater;private List <AppInfo> appinfos;//dynamic change appinfospublic void Setappinfos (list<appinfo> appinfos) {This.appinfos = Appinfos;} Public Appmanageradapter (Context context,list<appinfo> appinfos) {this.context = Context;this.appinfos = Appinfos;minflater = Layoutinflater.from (context);} 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 control//2 get data//3 bound Data View view = Null;if (Convertview! = null) {view = Convertview; }else{view = minflater.inflate (R.layout.applationinstall_item, null);} Gets the 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);//Get Position object AppInfo appInfo Position appInfo = Appinfos.get (position); Iv_appicon.setimagedrawable (Appinfo.getapp_icon ()); Tv_appname.settext (AppInfo.getApp_ Name ()); Tv_appversion.settext (Appinfo.getapp_version ()); return view;}}
7, the program display interface appmanageractivity
This class implements a very easy function, calls the methods of other classes, and displays the information obtained on the ListView.
The detailed implementation is to find the controls on the layout in the OnCreate method. The Appinfoservice method in a thread program gets the application installed in the phone and passes the obtained results through the handler and message mechanism to the main thread, which is displayed to the UI view by the main thread.
Detailed implementation code such as the following:
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;/** * App managed Manager class * @author Liuyazhuang * */public class Appmanageractivity Extends activity{protected static final int Success_get_applicaiton = 0;//The individual controls in the layout private relativelayout rl_loading; Private ListView lv_appmanage;private TextView tv_title;//Package Manager Private Packagemanager pm;//get mobile app information for business class private Appinfoservice appinfoservice;//mobile app app information collection private list<appinfo> appinfos;//user application information collection private list< Appinfo> userappinfos;//Whether it's all aPP program, default feel trueprivate boolean Isallapp = True;//appmanageradapter Adapter Object Private Appmanageradapter Madapter;private Popupwindow Mpopupwindow;//mhandler method Private Handler Mhandler = new Handler () {public void Handlemessage ( Android.os.Message msg) {switch (msg.what) {case success_get_applicaiton://to the ListView to bind the data. Hide loaded Controls Madapter = new Appmanageradapter (Getapplicationcontext (), appinfos);//Set Data Lv_appmanage.setadapter (MAdapter) ;//Hide Relativelayoutrl_loading.setvisibility (view.gone);//view.visible (Control Display) view.invisible (control hides but occupies space) View.gone (Control hide does not occupy space) Break;default:break;}};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.applationinstall);//Get control in 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 ();//Gets the application information for the phone installation in the child thread new thread () {public void run () {Appinfos = Appinfoservice.getappinfos (); Userappinfos = new arraylist<appinfo> (); for (AppInfo Appinfo:appinfos) {if (Appinfo.isuserapp ()) { Userappinfos.add (AppInfo);}} Message msg = new Message (); msg.what = Success_get_applicaiton;mhandler.sendmessage (msg);};}. Start ();}}
Add: Infer whether the application is a user program
Infer whether the application is a user program public boolean Filterapp (applicationinfo info) { //original is a system application. User manually upgrade if ((Info.flags & applicationinfo.flag_updated_system_app)! = 0) { return true; User-installed Applications } else if ((Info.flags & applicationinfo.flag_system) = = 0) { return true; } return false; }
Iii. Effect of implementation
Loading application in progress
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "height=" 340 "width=" 433 ">
Get the apps installed to your phone
Four, warm tips
In this example. For the sake of the aspect, I wrote some text directly in the layout file and related classes. Everyone in the real project to write these words in the Strings.xml file, in the external reference to these resources, remember, this is as an Android program ape the most important development knowledge and specifications, I am here just for the convenience of directly written in the class and layout files.
Android--Get mobile apps installed