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. }
相關文章
相關標籤/搜索