Android 獲取系統程序和應用程序
主要的功能
- 自定義Button(TextView來作Button)
- 經過點擊不一樣的Button顯示系統程序和應用程序
- 更改ListView選中時的背景色
PackageManager的功能:
•安裝,卸載應用
•查詢permission相關信息
•查詢Application相關信息(application,activity,receiver,service,provider及相應屬性等)
•查詢已安裝應用
•增長,刪除permission
•清除用戶數據、緩存,代碼段等
先看看效果圖,風格能夠本身調整
代碼就暫時不特別規範的寫了,只是測試程序
main.xml,總體佈局,默認TextView是沒有事件監聽的,須要設置其android:clickable屬性爲true
-
- <?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:background="@drawable/background_color"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="10dip"
- />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="40dip"
- >
- <TextView
- android:id="@+id/button01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:text="普通應用"
- android:gravity="center"
- android:background="@drawable/button_selector"
- android:focusable="true"
- android:clickable="true"
- android:layout_marginLeft="10dip"
- />
- <View android:layout_width="5px" android:layout_height="fill_parent"
- android:background="#FFFFFFFF"/>
- <TextView
- android:id="@+id/button02"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:text="系統應用"
- android:gravity="center"
- android:background="@drawable/button_selector"
- android:focusable="true"
- android:clickable="true"
- android:layout_marginRight="10dip"
- />
- </LinearLayout>
- <ListView
- android:id="@+id/lv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:fastScrollEnabled="true">
-
- </ListView>"
- </LinearLayout>
-
ListView的item佈局:listitem.xml,這裏ImageView的大小最好設置爲固定的,若是是wrap_content的話,不一樣應用程序的圖標不同大,顯示出來很難看
-
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/list_item_color_bg"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/appicon"
- android:layout_width="48dip"
- android:layout_height="48dip"
- android:padding="5dp"
- />
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:id="@+id/appName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/packageName"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- </LinearLayout>
-
下面的是drawable下的一些文件
background_color.xml
-
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
- <gradient
- android:startColor="#FFFFFFFF"
- android:endColor="#FFFFFFFF"
- android:angle="270.0"
- android:centerY="0.3"
- android:centerColor="#FFBDBDBD"
- />
- </shape>
-
button_color.xml
-
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
- <gradient
- android:startColor="#FF7F7F7F"
- android:endColor="#FF000000"
- android:angle="270.0"
- />
- </shape>
-
button_selector.xml,這是點擊TextView自定義的Button的selector文件
-
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android"
- android:constantSize="true" >
- <!-- 得到焦點時的背景圖片 -->
- <item
- android:state_focused="true"
- >
- <shape>
- <gradient
- android:startColor="#FFE5CF33"
- android:endColor="#FFF1E7A2"
- android:angle="90.0"
- />
- </shape>
- </item>
- <!-- 設置響應全部事件 -->
- <item android:state_enabled="true"
- android:state_pressed="false"
- >
- <shape>
- <gradient
- android:startColor="#FF1B1B1B"
- android:endColor="#FF969696"
- android:angle="90.0"
- />
- </shape>
- </item>
- <!-- 按鈕點擊時候的背景 -->
- <item android:state_enabled="true"
- android:state_pressed="true">
- <shape>
- <gradient
- android:startColor="#FF000000"
- android:endColor="#FF474747"
- android:angle="90.0"
- />
- </shape>
- </item>
- <item android:state_enabled="false"
- android:state_pressed="true">
- <shape>
- <gradient
- android:startColor="#FF000000"
- android:endColor="#ff474747"
- android:angle="90.0"
- />
- </shape>
- </item>
- <!-- 默認狀況下的背景 -->
- <item>
- <shape>
- <gradient
- android:startColor="#FF000000"
- android:endColor="#FF474747"
- android:angle="90.0"
- />
- </shape>
- </item>
- </selector>
-
list_item_color_bg.xml,這是點擊ListView時item自定義的選中背景
-
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:state_pressed="true"
- android:drawable="@color/green"
- ></item>
- <item
- android:drawable="@color/white"
- ></item>
- </selector>
-
這裏有幾點注意
- 點擊不一樣按鈕後須要切換不一樣的視圖,此時因爲數據源已經改變,因此須要點擊後當即變成改變後的結果,因此須要刷新ListView,使用adapter.notifyDataSetChanged()方法便可刷新ListView
- 系統應用程序和普通應用程序我放到了不一樣的ArrayList中了,若是感受這部分設計有問題的話,但願高手指點
-
- package com.loulijun.appshow;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class AppShowActivity extends Activity {
- /** Called when the activity is first created. */
- private TextView customAppsBtn, systemAppsBtn;
- private ListView lv;
- private List<PackageInfo> customApps; // 普通應用程序
- private List<PackageInfo> systemApps; // 系統應用程序
- MyAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- customAppsBtn = (TextView)findViewById(R.id.button01);
- systemAppsBtn = (TextView)findViewById(R.id.button02);
- lv = (ListView)findViewById(R.id.lv);
- //加載系統應用和普通應用程序
- loadApps();
- adapter = new MyAdapter(this, customApps);
- lv.setAdapter(adapter);
-
- customAppsBtn.setOnClickListener(new TextView.OnClickListener()
- {
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplicationContext(), "普通應用", Toast.LENGTH_SHORT).show();
- //切換到普通程序列表
- updateAppList(customApps);
- }
-
- });
- systemAppsBtn.setOnClickListener(new TextView.OnClickListener()
- {
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplicationContext(), "系統應用", Toast.LENGTH_SHORT).show();
- //切換到系統程序列表
- updateAppList(systemApps);
- }
-
- });
- }
-
- //列出普通應用程序
- private void loadApps()
- {
-
- customApps = new ArrayList<PackageInfo>();
- systemApps = new ArrayList<PackageInfo>();
- //獲得PackageManager對象
- PackageManager pm = this.getPackageManager();
- //獲得系統安裝的全部程序包的PackageInfo對象
- List<PackageInfo> packages = pm.getInstalledPackages(0);
- for(PackageInfo pi:packages)
- {
- //列出普通應用
- if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0)
- {
- customApps.add(pi);
- }
- //列出系統應用,老是感受這裏設計的有問題,但願高手指點
- if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)>0)
- {
- systemApps.add(pi);
- }
- }
- }
-
- private void updateAppList(List<PackageInfo> apps)
- {
- adapter.setData(apps);
- adapter.notifyDataSetChanged();
- }
-
- //ViewHolder靜態類
- static class ViewHolder
- {
- public ImageView appicon;
- public TextView appName;
- public TextView packageName;
- }
-
- class MyAdapter extends BaseAdapter
- {
- private LayoutInflater mInflater = null;
- private List<PackageInfo> apps;
- private MyAdapter(Context context, List<PackageInfo> apps)
- {
- this.mInflater = LayoutInflater.from(context);
- this.apps = apps;
- }
- //setData()要在MyAdapter類裏設置,用於設置數據源
- public void setData(List<PackageInfo> apps)
- {
- this.apps = apps;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return customApps.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
-
-
- if(convertView == null)
- {
- holder = new ViewHolder();
-
- convertView = mInflater.inflate(R.layout.list_item, null);
- holder.appicon = (ImageView)convertView.findViewById(R.id.appicon);
- holder.appName = (TextView)convertView.findViewById(R.id.appName);
- holder.packageName = (TextView)convertView.findViewById(R.id.packageName);
- convertView.setTag(holder);
- }else
- {
- holder = (ViewHolder)convertView.getTag();
- }
- PackageManager pm = getPackageManager(); // 獲得pm對象
- PackageInfo info = apps.get(position);
- ApplicationInfo appInfo = info.applicationInfo;
-
- holder.appicon.setImageDrawable(pm.getApplicationIcon(appInfo));
- holder.appName.setText(pm.getApplicationLabel(appInfo));
- holder.packageName.setText(appInfo.packageName);
- return convertView;
- }
-
- }
- }
歡迎關注本站公眾號,獲取更多信息