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. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:background="@drawable/background_color"
  6.     android:orientation="vertical" >
  7.     <LinearLayout 
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="10dip"
  10.         />
  11.     <LinearLayout 
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="40dip"
  14.         >
  15.         <TextView
  16.             android:id="@+id/button01"
  17.             android:layout_width="fill_parent"
  18.             android:layout_height="fill_parent"
  19.             android:layout_weight="1"
  20.             android:text="普通应用"
  21.             android:gravity="center"
  22.             android:background="@drawable/button_selector"
  23.             android:focusable="true"
  24.             android:clickable="true"
  25.             android:layout_marginLeft="10dip"
  26.             />
  27.         <View android:layout_width="5px" android:layout_height="fill_parent"
  28.             android:background="#FFFFFFFF"/>
  29.         <TextView
  30.             android:id="@+id/button02"
  31.             android:layout_width="fill_parent"
  32.             android:layout_height="fill_parent"
  33.             android:layout_weight="1"
  34.             android:text="系统应用"
  35.             android:gravity="center"
  36.             android:background="@drawable/button_selector"
  37.             android:focusable="true"
  38.             android:clickable="true"
  39.             android:layout_marginRight="10dip"
  40.             />
  41.     </LinearLayout>
  42.     <ListView 
  43.         android:id="@+id/lv"
  44.         android:layout_width="fill_parent"
  45.         android:layout_height="wrap_content"
  46.         android:fastScrollEnabled="true">
  47.         
  48.     </ListView>"
  49. </LinearLayout>

ListView的item布局:listitem.xml,这里ImageView的大小最好设置为固定的,如果是wrap_content的话,不同应用程序的图标不一样大,显示出来很难看

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="@drawable/list_item_color_bg"
  6.     android:orientation="horizontal" >
  7.     <ImageView 
  8.         android:id="@+id/appicon"
  9.         android:layout_width="48dip"
  10.         android:layout_height="48dip"
  11.         android:padding="5dp"
  12.         />
  13.     <LinearLayout 
  14.         android:orientation="vertical"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         >
  18.         <TextView 
  19.             android:id="@+id/appName"
  20.             android:layout_width="fill_parent"
  21.             android:layout_height="wrap_content"
  22.             />
  23.         <TextView
  24.             android:id="@+id/packageName"
  25.             android:layout_width="fill_parent"
  26.             android:layout_height="wrap_content"
  27.             />
  28.     </LinearLayout>
  29. </LinearLayout>

下面的是drawable下的一些文件
background_color.xml

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

button_color.xml

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

button_selector.xml,这是点击TextView自定义的Button的selector文件

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

list_item_color_bg.xml,这是点击ListView时item自定义的选中背景

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3.     <item
  4.         android:state_pressed="true"
  5.         android:drawable="@color/green"
  6.         ></item>
  7.     <item 
  8.         android:drawable="@color/white"
  9.         ></item>
  10. </selector>

这里有几点注意

  • 点击不同按钮后需要切换不同的视图,此时由于数据源已经改变,所以需要点击后立即变成改变后的结果,所以需要刷新ListView,使用adapter.notifyDataSetChanged()方法即可刷新ListView
  • 系统应用程序和普通应用程序我放到了不同的ArrayList中了,如果感觉这部分设计有问题的话,希望高手指点

  1. package com.loulijun.appshow;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

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

转载于:https://my.oschina.net/f839903061/blog/100863

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值