无码人妻精一区二区三区,eeuss影院www在线观看,无码精品久久久久久人妻中字,日韩av高清在线看片

推薦新聞
Android 獲取系統(tǒng)程序和應(yīng)用程序
發(fā)布者:深藍互聯(lián)
發(fā)布時間:2019-07-31
點擊:次
主要的功能 
  • 自定義Button(TextView來做Button)
  • 通過點擊不同的Button顯示系統(tǒng)程序和應(yīng)用程序
  • 更改ListView選中時的背景色
PackageManager的功能: 
  •安裝,卸載應(yīng)用 
  •查詢permission相關(guān)信息 
  •查詢Application相關(guān)信息(application,activity,receiver,service,provider及相應(yīng)屬性等) 
  •查詢已安裝應(yīng)用 
  •增加,刪除permission 
  •清除用戶數(shù)據(jù)、緩存,代碼段等 
先看看效果圖,風(fēng)格可以自己調(diào)整 
2012042321052318.png      2012042321055094.png  

代碼就暫時不特別規(guī)范的寫了,只是測試程序 

main.xml,整體布局,默認TextView是沒有事件監(jiān)聽的,需要設(shè)置其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="普通應(yīng)用"
  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="系統(tǒng)應(yīng)用"
  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的大小最好設(shè)置為固定的,如果是wrap_content的話,不同應(yīng)用程序的圖標不一樣大,顯示出來很難看
  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.     <!-- 設(shè)置響應(yīng)所有事件 -->
  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.  


這里有幾點注意 
 
  • 點擊不同按鈕后需要切換不同的視圖,此時由于數(shù)據(jù)源已經(jīng)改變,所以需要點擊后立即變成改變后的結(jié)果,所以需要刷新ListView,使用adapter.notifyDataSetChanged()方法即可刷新ListView
  • 系統(tǒng)應(yīng)用程序和普通應(yīng)用程序我放到了不同的ArrayList中了,如果感覺這部分設(shè)計有問題的話,希望高手指點
  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; // 普通應(yīng)用程序
  28.     private List<PackageInfo> systemApps; // 系統(tǒng)應(yīng)用程序
  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.         //加載系統(tǒng)應(yīng)用和普通應(yīng)用程序
  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(), "普通應(yīng)用", 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(), "系統(tǒng)應(yīng)用", Toast.LENGTH_SHORT).show();
  57.                 //切換到系統(tǒng)程序列表
  58.                 updateAppList(systemApps);
  59.             }
  60.             
  61.         });
  62.     }
  63.     
  64.     //列出普通應(yīng)用程序
  65.     private void loadApps()
  66.     {
  67.         
  68.         customApps = new ArrayList<PackageInfo>();
  69.         systemApps = new ArrayList<PackageInfo>();
  70.         //得到PackageManager對象
  71.         PackageManager pm = this.getPackageManager();
  72.         //得到系統(tǒng)安裝的所有程序包的PackageInfo對象
  73.         List<PackageInfo> packages = pm.getInstalledPackages(0);
  74.         for(PackageInfo pi:packages)
  75.         {
  76.             //列出普通應(yīng)用
  77.             if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0) 
  78.             {
  79.                 customApps.add(pi);
  80.             }
  81.             //列出系統(tǒng)應(yīng)用,總是感覺這里設(shè)計的有問題,希望高手指點
  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靜態(tài)類
  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類里設(shè)置,用于設(shè)置數(shù)據(jù)源
  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. }

 

 

關(guān)注深藍互聯(lián)公眾號
Copyright ? 2013-2025 深藍互聯(lián) 版權(quán)所有
友情鏈接: