2010年5月26日星期三

獲取已安裝軟件包(InstalledPackages)及其活動(Activity)的信息.

跟上一篇文章"Android 的功能編號(feature ID), PackageManager 和 getSystemAvailableFeatures()"差不多, 這個程序也是通過PackageManager獲得一些系統信息: 已安裝軟件包(InstalledPackages)及其活動(Activity).

首先程序會調用PackageManager.getInstalledPackages()獲取已安裝軟件包的信息, 並以列表視圖(ListView)形式顯示, 當點擊任何軟件包時, 會再以列表視圖(ListView)顯示其活動(Activity), 再當點擊任何活動時, 會以Toast顯示其名稱.

已安裝軟件包(InstalledPackages)信息
活動(Activity)信息

另一值得注意的是本程序只有一個列表活動(ListActivity), 一個列表視圖(ListView), 但有機會應用到兩個適配器上; 這可以在onListItemClick()中檢查ListView.getAdapter()從而辨認現在所使用的適配器.

main.xml
<?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"
>
<TextView
android:id="@+id/currentview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/reload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Reload InstalledPackages"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data" />
</LinearLayout>


AndroidInstalledPackages.java
package com.AndroidInstalledPackages;

import java.util.List;

import android.app.ListActivity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidInstalledPackages extends ListActivity {

List<PackageInfo> PackageInfoList;
ArrayAdapter<PackageInfo> PackageArrayAdapter;
ActivityInfo[] ActivityInfoArray;
ArrayAdapter<ActivityInfo> ActivityInfoAdapter;

TextView currentView;
Button buttonReload;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
currentView = (TextView)findViewById(R.id.currentview);
buttonReload = (Button)findViewById(R.id.reload);

loadPackageInfoList();

buttonReload.setOnClickListener( new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadPackageInfoList();
}
});

}

private void loadPackageInfoList(){
PackageInfoList = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
PackageArrayAdapter = new ArrayAdapter<PackageInfo>(this, R.layout.list, PackageInfoList);
setListAdapter(PackageArrayAdapter);

currentView.setText("getInstalledPackages");
buttonReload.setEnabled(false);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);

if (l.getAdapter()==PackageArrayAdapter){
ActivityInfoArray = PackageInfoList.get(position).activities;
if (ActivityInfoArray!=null)
{
ActivityInfoAdapter = new ArrayAdapter<ActivityInfo>(this, R.layout.list, ActivityInfoArray);
setListAdapter(ActivityInfoAdapter);

currentView.setText("activities");
buttonReload.setEnabled(true);
}
else{
Toast.makeText(this, "Empty", Toast.LENGTH_LONG).show();
}
}
else{
CharSequence info = ActivityInfoArray[position].name;
Toast.makeText(this, info, Toast.LENGTH_LONG).show();
}

}

}

沒有留言:

發佈留言