深蓝互联专注深圳物联网方案开发定制、小程序开发、SaaS系统、APP开发和深圳软硬件方案公司欢迎咨询邮箱:wisepu@szdbi.com   电话:13530005652   联系我们   |   网站地图   



Android 获取系统程序和应用程序

来源:     发布:     点击:
主要的功能 
  • 自定义Button(TextView来做Button)
  • 通过点击不同的Button显示系统程序和应用程序
  • 更改ListView选中时的背景色
PackageManager的功能: 
  •安装,卸载应用 
  •查询permission相关信息 
  •查询Application相关信息(application,activity,receiver,service,provider及相应属性等) 
  •查询已安装应用 
  •增加,删除permission 
  •清除用户数据、缓存,代码段等 
先看看效果图,风格可以自己调整 
2012042321052318.png      2012042321055094.png  

代码就暂时不特别规范的写了,只是测试程序 

main.xml,整体布局,默认TextView是没有事件监听的,需要设置其android:clickable属性为true
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:background="@drawable/background_color"
  7.     android:orientation="vertical" >
  8.     <LinearLayout 
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="10dip"
  11.         />
  12.     <LinearLayout 
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="40dip"
  15.         >
  16.         <TextView
  17.             android:id="@+id/button01"
  18.             android:layout_width="fill_parent"
  19.             android:layout_height="fill_parent"
  20.             android:layout_weight="1"
  21.             android:text="普通应用"
  22.             android:gravity="center"
  23.             android:background="@drawable/button_selector"
  24.             android:focusable="true"
  25.             android:clickable="true"
  26.             android:layout_marginLeft="10dip"
  27.             />
  28.         <View android:layout_width="5px" android:layout_height="fill_parent"
  29.             android:background="#FFFFFFFF"/>
  30.         <TextView
  31.             android:id="@+id/button02"
  32.             android:layout_width="fill_parent"
  33.             android:layout_height="fill_parent"
  34.             android:layout_weight="1"
  35.             android:text="系统应用"
  36.             android:gravity="center"
  37.             android:background="@drawable/button_selector"
  38.             android:focusable="true"
  39.             android:clickable="true"
  40.             android:layout_marginRight="10dip"
  41.             />
  42.     </LinearLayout>
  43.     <ListView 
  44.         android:id="@+id/lv"
  45.         android:layout_width="fill_parent"
  46.         android:layout_height="wrap_content"
  47.         android:fastScrollEnabled="true">
  48.         
  49.     </ListView>"
  50. </LinearLayout>
  51.  


ListView的item布局:listitem.xml,这里ImageView的大小最好设置为固定的,如果是wrap_content的话,不同应用程序的图标不一样大,显示出来很难看
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:background="@drawable/list_item_color_bg"
  7.     android:orientation="horizontal" >
  8.     <ImageView 
  9.         android:id="@+id/appicon"
  10.         android:layout_width="48dip"
  11.         android:layout_height="48dip"
  12.         android:padding="5dp"
  13.         />
  14.     <LinearLayout 
  15.         android:orientation="vertical"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="wrap_content"
  18.         >
  19.         <TextView 
  20.             android:id="@+id/appName"
  21.             android:layout_width="fill_parent"
  22.             android:layout_height="wrap_content"
  23.             />
  24.         <TextView
  25.             android:id="@+id/packageName"
  26.             android:layout_width="fill_parent"
  27.             android:layout_height="wrap_content"
  28.             />
  29.     </LinearLayout>
  30. </LinearLayout>
  31.  


下面的是drawable下的一些文件 
background_color.xml
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  4.     <gradient
  5.         android:startColor="#FFFFFFFF"
  6.         android:endColor="#FFFFFFFF"
  7.         android:angle="270.0"
  8.         android:centerY="0.3"
  9.         android:centerColor="#FFBDBDBD"
  10.         />
  11. </shape>
  12.  


button_color.xml
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  4.     <gradient 
  5.         android:startColor="#FF7F7F7F"
  6.         android:endColor="#FF000000"
  7.         android:angle="270.0"
  8.         />
  9. </shape>
  10.  


button_selector.xml,这是点击TextView自定义的Button的selector文件
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:constantSize="true" >
  5.     <!-- 获得焦点时的背景图片 -->
  6.     <item 
  7.         android:state_focused="true"
  8.         >
  9.         <shape>
  10.             <gradient 
  11.                 android:startColor="#FFE5CF33"
  12.                 android:endColor="#FFF1E7A2"
  13.                 android:angle="90.0"
  14.                 />
  15.         </shape>
  16.     </item>
  17.     <!-- 设置响应所有事件 -->
  18.     <item android:state_enabled="true"
  19.         android:state_pressed="false"
  20.         >
  21.         <shape>
  22.             <gradient 
  23.                 android:startColor="#FF1B1B1B"
  24.                 android:endColor="#FF969696"
  25.                 android:angle="90.0"
  26.                 />
  27.         </shape>
  28.     </item>
  29.     <!-- 按钮点击时候的背景 -->
  30.     <item android:state_enabled="true"
  31.         android:state_pressed="true">
  32.         <shape>
  33.             <gradient 
  34.                 android:startColor="#FF000000"
  35.                 android:endColor="#FF474747"
  36.                 android:angle="90.0"
  37.                 />
  38.         </shape>
  39.     </item>
  40.     <item android:state_enabled="false"
  41.         android:state_pressed="true">
  42.         <shape>
  43.             <gradient 
  44.                 android:startColor="#FF000000"
  45.                 android:endColor="#ff474747"
  46.                 android:angle="90.0"
  47.                 />
  48.         </shape>
  49.     </item>
  50.     <!-- 默认情况下的背景 -->
  51.     <item>
  52.         <shape>
  53.             <gradient 
  54.                 android:startColor="#FF000000"
  55.                 android:endColor="#FF474747"
  56.                 android:angle="90.0"
  57.                 />
  58.         </shape>
  59.     </item>
  60. </selector>
  61.  


list_item_color_bg.xml,这是点击ListView时item自定义的选中背景
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  4.     <item
  5.         android:state_pressed="true"
  6.         android:drawable="@color/green"
  7.         ></item>
  8.     <item 
  9.         android:drawable="@color/white"
  10.         ></item>
  11. </selector>
  12.  


这里有几点注意 
 
  • 点击不同按钮后需要切换不同的视图,此时由于数据源已经改变,所以需要点击后立即变成改变后的结果,所以需要刷新ListView,使用adapter.notifyDataSetChanged()方法即可刷新ListView
  • 系统应用程序和普通应用程序我放到了不同的ArrayList中了,如果感觉这部分设计有问题的话,希望高手指点
  1.  
  2. package com.loulijun.appshow;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.content.pm.ApplicationInfo;
  12. import android.content.pm.PackageInfo;
  13. import android.content.pm.PackageManager;
  14. import android.os.Bundle;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.BaseAdapter;
  19. import android.widget.ImageView;
  20. import android.widget.ListView;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23. public class AppShowActivity extends Activity {
  24.     /** Called when the activity is first created. */
  25.     private TextView customAppsBtn, systemAppsBtn;
  26.     private ListView lv;
  27.     private List<PackageInfo> customApps; // 普通应用程序
  28.     private List<PackageInfo> systemApps; // 系统应用程序
  29.     MyAdapter adapter;
  30.     @Override
  31.     public void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.main);
  34.         customAppsBtn = (TextView)findViewById(R.id.button01);
  35.         systemAppsBtn = (TextView)findViewById(R.id.button02);
  36.         lv = (ListView)findViewById(R.id.lv);
  37.         //加载系统应用和普通应用程序
  38.         loadApps();
  39.         adapter = new MyAdapter(this, customApps);
  40.         lv.setAdapter(adapter);
  41.         
  42.         customAppsBtn.setOnClickListener(new TextView.OnClickListener()
  43.         {
  44.             @Override
  45.             public void onClick(View v) {
  46.                 Toast.makeText(getApplicationContext(), "普通应用", Toast.LENGTH_SHORT).show();
  47.                 //切换到普通程序列表
  48.                 updateAppList(customApps);
  49.             }
  50.             
  51.         });
  52.         systemAppsBtn.setOnClickListener(new TextView.OnClickListener()
  53.         {
  54.             @Override
  55.             public void onClick(View v) {
  56.                 Toast.makeText(getApplicationContext(), "系统应用", Toast.LENGTH_SHORT).show();
  57.                 //切换到系统程序列表
  58.                 updateAppList(systemApps);
  59.             }
  60.             
  61.         });
  62.     }
  63.     
  64.     //列出普通应用程序
  65.     private void loadApps()
  66.     {
  67.         
  68.         customApps = new ArrayList<PackageInfo>();
  69.         systemApps = new ArrayList<PackageInfo>();
  70.         //得到PackageManager对象
  71.         PackageManager pm = this.getPackageManager();
  72.         //得到系统安装的所有程序包的PackageInfo对象
  73.         List<PackageInfo> packages = pm.getInstalledPackages(0);
  74.         for(PackageInfo pi:packages)
  75.         {
  76.             //列出普通应用
  77.             if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0) 
  78.             {
  79.                 customApps.add(pi);
  80.             }
  81.             //列出系统应用,总是感觉这里设计的有问题,希望高手指点
  82.             if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)>0) 
  83.             {
  84.                 systemApps.add(pi);
  85.             }
  86.         }
  87.     }
  88.   
  89.     private void updateAppList(List<PackageInfo> apps)
  90.     {
  91.         adapter.setData(apps);
  92.         adapter.notifyDataSetChanged();
  93.     }
  94.     
  95.     //ViewHolder静态类
  96.     static class ViewHolder
  97.     {
  98.         public ImageView appicon;
  99.         public TextView appName;
  100.         public TextView packageName;
  101.     }
  102.     
  103.     class MyAdapter extends BaseAdapter
  104.     {
  105.         private LayoutInflater mInflater = null;
  106.         private List<PackageInfo> apps;
  107.         private MyAdapter(Context context, List<PackageInfo> apps)
  108.         {
  109.             this.mInflater = LayoutInflater.from(context);            
  110.             this.apps = apps;
  111.         }
  112.         //setData()要在MyAdapter类里设置,用于设置数据源
  113.         public void setData(List<PackageInfo> apps)
  114.         {
  115.             this.apps = apps;
  116.         }
  117.         @Override
  118.         public int getCount() {
  119.             // TODO Auto-generated method stub
  120.             return customApps.size();
  121.         }
  122.         @Override
  123.         public Object getItem(int position) {
  124.             // TODO Auto-generated method stub
  125.             return position;
  126.         }
  127.         @Override
  128.         public long getItemId(int position) {
  129.             // TODO Auto-generated method stub
  130.             return position;
  131.         }
  132.         @Override
  133.         public View getView(int position, View convertView, ViewGroup parent) {
  134.             ViewHolder holder = null;
  135.             
  136.             
  137.             if(convertView == null)
  138.             {
  139.                 holder = new ViewHolder();
  140.     
  141.                 convertView = mInflater.inflate(R.layout.list_item, null);
  142.                 holder.appicon = (ImageView)convertView.findViewById(R.id.appicon);
  143.                 holder.appName = (TextView)convertView.findViewById(R.id.appName);
  144.                 holder.packageName = (TextView)convertView.findViewById(R.id.packageName);
  145.                 convertView.setTag(holder);
  146.             }else
  147.             {
  148.                 holder = (ViewHolder)convertView.getTag();
  149.             }
  150.             PackageManager pm = getPackageManager(); // 得到pm对象
  151.             PackageInfo info = apps.get(position);
  152.             ApplicationInfo appInfo = info.applicationInfo;
  153.             
  154.             holder.appicon.setImageDrawable(pm.getApplicationIcon(appInfo));
  155.             holder.appName.setText(pm.getApplicationLabel(appInfo));
  156.             holder.packageName.setText(appInfo.packageName);
  157.             return convertView;
  158.         }
  159.         
  160.     }
  161. }

 

深蓝互联成立于2013年,是一家物联网硬件开发及软件应用服务商,获得多次获得国家高新技术企业资质的企业。深蓝互联专注软硬件技术开发的专业性技术公司。我们从事软硬件开发十年,擅长SaaS 平台开发、APP小程序开发、软硬件结合开发,在视觉识别处理、数据架构、云计算、多线程高并发和集群、数据安全加密和防护方便有很深的技术积累。

 

我们拥有专业优秀的设计和技术团队,以极具创意的 UI 设计、精湛卓越的开发技术,专业的网络策划团队。公司多年来投入打造物联网SaaS平台,集成了公司研发的多款智能物联网终端(智能鲜米机、生鲜售货机、自助洗车机、小区电瓶车充电系统等)。

 

公司一直坚持以研发为导向,打造软硬件结合的物联网平台系统。将一直坚持提高开发的技术实力更好的为我们的客户服务!

 

文章来自深蓝互联http://www.szdbi.com/WEBkaifajishu/283.html转载请注明出处!

相关文章

无相关信息


深圳市龙华新区龙观西路2号宝龙大厦A903(与布龙路交汇处)