歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 獲取系統和應用程序

PackageManager是個非常好的東西,其他的詳細的細節等日後有時間整理
        PackageManager的功能:
        安裝,卸載應用
        查詢permission相關信息
        查詢Application相關信息(application,activity,receiver,service,provider及相應屬性等)
        查詢已安裝應用
        增加,刪除permission

        清除用戶數據、緩存,代碼段等

        我們可以用PackageManager來顯示系統安裝的應用程序列表或者系統程序列表

package eoe.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.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class AppShowActivity extends Activity {
ListView lv;
MyAdapter adapter;
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.lv);
//得到PackageManager對象
PackageManager pm = getPackageManager();
//得到系統安裝的所有程序包的PackageInfo對象
//List<ApplicationInfo> packs = pm.getInstalledApplications(0);
List<PackageInfo> packs = pm.getInstalledPackages(0);
for(PackageInfo pi:packs)
{
HashMap<String, Object> map = new HashMap<String, Object>();
//顯示用戶安裝的應用程序,而不顯示系統程序
// if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0&&
// (pi.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)==0)
// {
// //這將會顯示所有安裝的應用程序,包括系統應用程序
// map.put("icon", pi.applicationInfo.loadIcon(pm));//圖標
// map.put("appName", pi.applicationInfo.loadLabel(pm));//應用程序名稱
// map.put("packageName", pi.applicationInfo.packageName);//應用程序包名
// //循環讀取並存到HashMap中,再增加到ArrayList上,一個HashMap就是一項
// items.add(map);
// }
//這將會顯示所有安裝的應用程序,包括系統應用程序
map.put("icon", pi.applicationInfo.loadIcon(pm));//圖標
map.put("appName", pi.applicationInfo.loadLabel(pm));//應用程序名稱
map.put("packageName", pi.applicationInfo.packageName);//應用程序包名
//循環讀取並存到HashMap中,再增加到ArrayList上,一個HashMap就是一項
items.add(map);
}


/**
* 參數:Context
* ArrayList(item的集合)
* item的layout
* 包含ArrayList中的HashMap的key的數組
* key所對應的值的相應的控件id
*/
adapter = new MyAdapter(this, items, R.layout.piitem,
new String[]{"icon", "appName", "packageName"},
new int[]{R.id.icon, R.id.appName, R.id.packageName});
lv.setAdapter(adapter);
}
}

class MyAdapter extends SimpleAdapter
{
private int[] appTo;
private String[] appFrom;
private ViewBinder appViewBinder;
private List<? extends Map<String, ?>> appData;
private int appResource;
private LayoutInflater appInflater;
public MyAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
appData = data;
appResource = resource;
appFrom = from;
appTo = to;
appInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public View getView(int position, View convertView, ViewGroup parent){
return createViewFromResource(position, convertView, parent, appResource);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource){
View v;
if(convertView == null)
{
v = appInflater.inflate(resource, parent,false);
final int[] to = appTo;
final int count = to.length;
final View[] holder = new View[count];
for(int i = 0; i < count; i++)
{
holder[i] = v.findViewById(to[i]);
}
v.setTag(holder);
}else
{
v = convertView;
}
bindView(position, v);
return v;
}


private void bindView(int position, View view)
{
final Map dataSet = appData.get(position);
if(dataSet == null)
{
return;
}
final ViewBinder binder = appViewBinder;
final View[] holder = (View[])view.getTag();
final String[] from = appFrom;
final int[] to = appTo;
final int count = to.length;
for(int i = 0; i < count; i++)
{
final View v = holder[i];
if(v != null)
{
final Object data = dataSet.get(from[i]);
String text = data == null ? "":data.toString();
if(text == null)
{
text = "";
}
boolean bound = false;
if(binder != null)
{
bound = binder.setViewValue(v, data, text);
}
if(!bound)
{
/**
* 自定義適配器,關在在這裡,根據傳遞過來的控件以及值的數據類型,
* 執行相應的方法,可以根據自己需要自行添加if語句。另外,CheckBox等
* 集成自TextView的控件也會被識別成TextView,這就需要判斷值的類型
*/


if(v instanceof TextView)
{
//如果是TextView控件,則調用SimpleAdapter自帶的方法,設置文本
setViewText((TextView)v, text);
}else if(v instanceof ImageView){
//如果是ImageView控件,調用自己寫的方法,設置圖片
setViewImage((ImageView)v, (Drawable)data);
}else{
throw new IllegalStateException(v.getClass().getName() + " is not a " + "view that can be bounds by this SimpleAdapter");
}
}
}
}
}
public void setViewImage(ImageView v, Drawable value){
v.setImageDrawable(value);
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/lv" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="48dip"
android:layout_height="48dip" />
<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>
Copyright © Linux教程網 All Rights Reserved