Android--get Mobile Apps installed

Source: Internet
Author: User
<span id="Label3"></p><pre><pre>Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47114331</pre></pre><p><p>In the last few blog posts about android, i'll show you some of the useful features that are commonly used in several projects, so in this blog post, i'll show you how to get a mobile app That's already installed on your Android phone, as well as a practical way to use the Golden Hill Mobile defender or the 360-cell phone Defender's kids Shoes. These software are available for applications that are installed in the current PHONE. So how are these features implemented? Let's take a look at how these features are Implemented.</p></p>first, the principle<p><p>The principle is simple, we use the Packagemanager class provided in android, to get the application information installed on the phone, to encapsulate this information into an object, which contains the Application's icon, name, version number, package name, Whether it is a user application and other information, These objects are then encapsulated into a collection of objects, and the collection is displayed on the listview of the interface, forming an application List. Presented to the user is a list of applications installed in the PHONE.</p></p><p><p>The principle is not very simple? next, Let's implement these features Together.</p></p>Ii. implement 1, Create an application entity class AppInfo<p><p>For more object-oriented and object-oriented encapsulation, each application information I get is encapsulated into a Java object that contains information such as the Application's icon, name, version number, package name, and whether it is a user Application.</p></p><p><p>The specific implementation code is as Follows:</p></p><p><p></p></p><pre name="code" class="java">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 number 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 + "]";}}</pre><p><p></p></p>2. Get the business class of the mobile app Appinfoservice<p><p>This class primarily implements the main business functions of getting the applications installed in the phone, encapsulating how to get the apps installed by the PHONE.</p></p><p><p>The specific implementation code is as Follows:</p></p><p><p></p></p><pre name="code" class="java">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 in the 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 number of the app PackageInfo PackageInfo = Pm.getpackageinfo (packagename, 0); String app_version = packageinfo.versionname;appinfo.setapp_version (app_version);} Catch (namenotfoundexception e) {e.printstacktrace ();} Determine if the application is a user program, Boolean isuserapp = Filterapp (info), appinfo.setuserapp (isuserapp); appinfos.add (appInfo);} Return appinfos;} Determine if 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; }}</pre><p><p></p></p>3. Style file Styles.xml<p><p>Create a new Styles.xml file in the Res/values directory to define the style information for the application, and I'll define two styles in this File.</p></p><p><p>The specific code is as Follows:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><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 ></pre></pre><pre name="code" class="html"><pre name="code" class="html"><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></pre></pre><p><p></p></p>4. Main layout file Applationinstall.xml<p><p>The specific implementation code is as Follows:</p></p><p><p></p></p><pre name="code" class="html"><?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 "&G T <textview android:id= "@+id/tv_title" style= "@style/text_title_style" android:text= "all programs"/> & Lt View style= "@style/view_divide_line_style"/> <framelayout android:layout_width= "fill_parent" Andro id:layout_height= "fill_parent" > <!--android:cachecolorhint= "#00000000" cache color default is yellow Android:div ider= "#00ffffff" Split line android:dividerheight= "3.0dip" split 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= "fil L_parent "> <linearlayout android:layout_width=" wrap_content "android:layou t_height= "wrap_content" android:orientation= "vertical" android:layout_centerinparent= "true" > <progressbar android:id= "@+id/pb" android:layout_width= "wrap_ Content "android:layout_height=" wrap_content "android:layout_gravity=" Center " /> <textview android:layout_width= "wrap_content" A ndroid:layout_height= "wrap_content" android:layout_margintop= "10dip" android:text= "positive In the Load application: " android:textcolor= "#fff" android:textsize= "22sp"/> </LinearLayout> &L T;/relativelayout> </FrameLayout></LinearLayout></pre><p><p></p></p>5, each item in the ListView layout Applationinstall_item.xml<p><p>The specific implementation code is as follows</p></p><p><p></p></p><pre name="code" class="html"><?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" android:paddingleft= "6.0dip" android:paddingright= "5.0dip" android:paddingtop= "8.0dip" > <imageview android:id= "@+id/iv_appicon" android:layout_width= "wrap_content" Andro id:layout_height= "wrap_content" android:layout_centervertical= "true" android:src= "@DRAWABLE/IC_LAUNCHER"/&G T <textview android:id= "@+id/tv_appname" android:layout_width= "wrap_content" android:layout_height= " Wrap_content "android:layout_centervertical=" true "android:layout_marginleft=" 4.0dip "android:layout_ torightof= "@id/iv_appicon" android:textcolor= "#fff" android:text= "i swing" android:textsize= "16.0dip"/& Gt <textview android:id= "@+id/tv_appversion" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignparentright= "true" android:layout_centervertical= "true" Android:layout_marginlef t= "4.0dip" android:textcolor= "#fff" android:layout_marginright= "10dp" android:text= "1.0" Andro Id:textsize= "16.0dip"/></relativelayout></pre><p><p></p></p>6. Custom ListView Adapter Appmanageradapter<p><p>This class inherits from Baseadapter primarily as the adapter for the list display data, in which the layout loader layoutinflater loads the entry layout and finds controls on the layout to set the appropriate Information.</p></p><p><p>The specific implementation code is as Follows:</p></p><p><p></p></p><pre name="code" class="java">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;}}</pre><p><p></p></p>7, The program display interface appmanageractivity<p><p>This class implements a simple function that calls the methods of other classes and displays the information obtained on the LISTVIEW. The specific implementation is to find the control on the layout in the OnCreate method, and invoke the method in a thread program to get the application installed in the phone, and pass the obtained results through handler and the message mechanism to the main thread. The main thread displays the data on the UI View.</p></p><p><p>The specific implementation code is as Follows:</p></p><p><p></p></p><pre name="code" class="java">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 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 data, hide the loaded control Madapter = new Appmanageradapter (getapplicationcontext (), appinfos);//set data lv_appmanage.setadapter (madapter);// Hide relativelayoutrl_loading.setvisibility (view.gone);//view.visible (control display) view.invisible (control hidden 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 ();}}</pre><p><p></p></p>third, the Operation effect<p><p>Loading application<br></p></p><p><p></p></p><p><p>Get the apps installed to your phone<br></p></p><p><p></p></p>four, Warm Tips<p><p><span style="font-size:18px">In this example, for the sake of the aspect, I write some text directly in the layout file and the related class, Everyone in the real project to write these words in the Strings.xml file, in the external reference to these resources, remember, this is the most basic development knowledge and specifications as an Android programmer, I am here just to facilitate directly written in the class and layout files.</span><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--get Mobile Apps installed</p></p></span>

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.