Android獲取手機應用程序包的信息

     今天寫了一個獲取手機程序相關信息的demo,如下是demo的運行結果。
java

     這是項目的結構。android

下面是項目的具體代碼:緩存

1.MainActivityapp

package com.example.adapterlearn.view;ide


import java.util.ArrayList;this

import java.util.List;spa


import android.app.Activity;.net

import android.content.Context;xml

import android.content.pm.PackageInfo;ip

import android.content.pm.PackageManager;

import android.os.Bundle;

import android.view.Window;

import android.widget.ListView;


import com.example.adapterlearn.R;

import com.example.adapterlearn.model.MyPackageInfo;

import com.example.adapterlearn.view.controls.MyAdapter;


/**

 * 顯示手機應用程序包的相關信息

 * 

 * @做者: 劉倩</br>

 * @時間: 2014年4月23日 下午5:36:49</br>

 * @描述: 主要顯示應用程序包的相關信息</br>

 */

public class MainActivity extends Activity

{

/** 聲明ListView */

private ListView listView;

/** 包的數據集合 */

private List<MyPackageInfo> piList;

/** 上下文 */

private Context context;


@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

// 設置爲無標題狀態

requestWindowFeature(Window.FEATURE_NO_TITLE);

initView();

}


/**

* 初始化視圖

*/

private void initView()

{

context = this;

setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.acitvity_main_listview);

piList = getPhonePackageInfo();

listView.setAdapter(new MyAdapter(context, piList));

}


/**

* 獲得包的詳細信息

* @return 返回包的詳細信息

*/

private List<MyPackageInfo> getPhonePackageInfo()

{

List<MyPackageInfo> myList = new ArrayList<MyPackageInfo>();

PackageManager packageManager = context.getPackageManager();

List<PackageInfo> appList = getAllPackageInfoFromThePhone();

for (int i = 0; i < appList.size(); i++)

{

PackageInfo pinfo = appList.get(i);

MyPackageInfo myPackageInfo = new MyPackageInfo();

// 設置icon

myPackageInfo.setPackageIcon(packageManager

.getApplicationIcon(pinfo.applicationInfo));

// 設置應用名稱

myPackageInfo.setPackageLabel(packageManager.getApplicationLabel(

pinfo.applicationInfo).toString());

// 設置包名稱

myPackageInfo.setPackageName(pinfo.applicationInfo.packageName);

myList.add(myPackageInfo);

}

return myList;

}


/**

* 獲得全部的包信息

* @return 返回手機中包的信息

*/

@SuppressWarnings("static-access")

private List<PackageInfo> getAllPackageInfoFromThePhone()

{

List<PackageInfo> apkList = new ArrayList<PackageInfo>();

PackageManager packageManager = context.getPackageManager();

List<PackageInfo> list = packageManager.getInstalledPackages(0);

for (int i = 0; i < list.size(); i++)

{

PackageInfo pi = list.get(i);

if ((pi.applicationInfo.flags & pi.applicationInfo.FLAG_SYSTEM) <= 0)

{

apkList.add(pi);

}

}

return apkList;

}


}

2.MyAdapter

package com.example.adapterlearn.view.controls;


import java.util.List;

import android.annotation.SuppressLint;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import com.example.adapterlearn.R;

import com.example.adapterlearn.model.MyPackageInfo;


/**

 * 顯示應用程序的適配器

 * 

 * @做者: 劉倩</br>

 * @時間: 2014年4月23日 下午5:34:34</br>

 * @描述: 該適配器主要顯示應用程序的icon,應用名和包名</br>

 */

public class MyAdapter extends BaseAdapter

{

/** 上下文 */

Context context;

/** 包信息 */

List<MyPackageInfo> piList;

/** 填充器 */

LayoutInflater mInflater;


public MyAdapter(Context context, List<MyPackageInfo> piList)

{

this.context = context;

this.piList = piList;

this.mInflater = LayoutInflater.from(context);

}


@Override

public int getCount()

{

return piList.size();

}


@Override

public Object getItem(int position)

{

return piList.get(position);

}


@Override

public long getItemId(int position)

{

return position;

}


@SuppressLint("NewApi")

@Override

public View getView(int position, View convertView, ViewGroup parent)

{

ViewHolder viewHolder;

if (convertView == null)

{

convertView = mInflater.inflate(R.layout.activity_main_adapter,

null);

viewHolder = new ViewHolder();

viewHolder.ivShowIcon = (ImageView) convertView

.findViewById(R.id.activity_main_adapter_iv_icon);

viewHolder.tvShowLabel = (TextView) convertView

.findViewById(R.id.activity_main_adapter_tv_label);

viewHolder.tvShowPackageName = (TextView) convertView

.findViewById(R.id.activity_main_adapter_tv_packagename);

convertView.setTag(viewHolder);

} else

{

viewHolder = (ViewHolder) convertView.getTag();

}

viewHolder.ivShowIcon.setBackground(piList.get(position)

.getPackageIcon());

viewHolder.tvShowLabel.setText(piList.get(position).getPackageLabel());

viewHolder.tvShowPackageName.setText(piList.get(position)

.getPackageName());

return convertView;

}


/**

* 存放控件的類

* @做者: 劉倩</br>

* @時間: 2014年4月23日 下午5:34:04</br>

* @描述: 此類用於存放控件,緩存用</br>

*/

class ViewHolder

{

public ImageView ivShowIcon;

public TextView tvShowLabel;

public TextView tvShowPackageName;

}

}

3.MyPackageInfo

package com.example.adapterlearn.model;


import android.graphics.drawable.Drawable;


/**

 * 自定義的程序包的信息類

 * 

 * @做者: 劉倩</br>

 * @時間: 2014年4月23日 下午5:35:43</br>

 * @描述: 這裏只定義了程序包的icon,應用名稱和包名</br>

 */

public class MyPackageInfo

{

/** 應用程序的包名 */

private String packageName = "";

/** 應用程序的icon */

private Drawable packageIcon;

/** 應用程序的標籤 */

private String packageLabel = "";


public String getPackageName()

{

return packageName;

}


public void setPackageName(String packageName)

{

this.packageName = packageName;

}


public Drawable getPackageIcon()

{

return packageIcon;

}


public void setPackageIcon(Drawable drawable)

{

this.packageIcon = drawable;

}


public String getPackageLabel()

{

return packageLabel;

}


public void setPackageLabel(String packageLabel)

{

this.packageLabel = packageLabel;

}


}

4.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >


    <ListView

        android:id="@+id/acitvity_main_listview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


</RelativeLayout>

5.activity_main_adapter.xml

<?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:orientation="horizontal" >


    <ImageView

        android:id="@+id/activity_main_adapter_iv_icon"

        android:layout_width="50dip"

        android:layout_height="50dip"

        android:layout_marginBottom="5dip"

        android:layout_marginTop="5dip"

        android:contentDescription="@null" />


    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginBottom="5dip"

        android:layout_marginLeft="10dip"

        android:layout_marginTop="5dip"

        android:orientation="vertical" >


        <TextView

            android:id="@+id/activity_main_adapter_tv_label"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />


        <TextView

            android:id="@+id/activity_main_adapter_tv_packagename"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

以上是所有代碼,若是想下載源碼,請進入:http://download.csdn.net/detail/u010820846/7240785下載

相關文章
相關標籤/搜索