2013年8月8日 星期四

getApplicationIcon (取得應用程式圖示)

getApplicationIcon (取得應用程式圖示)

利用 getApplicationIcon () 取得App圖示+getApplicationLabel() 取得App標題.

實作完成的畫面如下:

method 說明:
abstract CharSequencegetApplicationLabel(ApplicationInfo info)
Return the label to use for this application.
abstract DrawablegetApplicationIcon(ApplicationInfo info)
Retrieve the icon associated with an application.
abstract ApplicationInfogetApplicationInfo(String packageName, int flags)
Retrieve all of the information we know about a particular package/application.
abstract List<ApplicationInfo>getInstalledApplications(int flags)
Return a List of all application packages that are installed on the device.



當你在看這篇之前, 你需要先會:
以下是我的操作流程:

Step 1:
建立一個空白的專案.
Application name, 叫GetApplicationIcon 測試.


Step 2:
修改 layout目錄下的 activity_main.xml , 使用下面的設定值:
<LinearLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/app_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>


Step 3:
主程式 MainActivity.java , 加入下面 "紅字" 的程式碼.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    ImageView myImageView = (ImageView)findViewById(R.id.imageview);
    PackageManager pm = getPackageManager();
    List<ApplicationInfo> apps = pm.getInstalledApplications(0);
    List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
    for(ApplicationInfo app : apps) {
        installedApps.add(app);
        String label = (String)pm.getApplicationLabel(app);
        if(label.equalsIgnoreCase("Facebook"))
        {
            TextView app_label = (TextView)findViewById(R.id.app_label);
            app_label.setText(label);
            Drawable icon = pm.getApplicationIcon(app);
            myImageView.setImageDrawable(icon);
        }
    }
}

附註: 貼上程式碼到Eclipse後, 有幾個Error 的地方需要手動點一下, import. (太簡單了, 略過貼圖, 操作的圖片, 可以參考看看前幾篇文章).



# 後記, 使用方法2, 透過 packageInfo 來存取
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView myImageView = (ImageView)findViewById(R.id.imageview);
    PackageManager pm = getPackageManager();
    List<PackageInfo> packs = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

    for (PackageInfo pi : packs) {
        String label = (String) pi.applicationInfo.loadLabel(pm);
        Log.d("Application Label:", label);
        if(label.equalsIgnoreCase("Facebook"))
        {
            TextView app_label = (TextView)findViewById(R.id.app_label);
            app_label.setText(label);

            TextView app_packagename = (TextView)findViewById(R.id.app_packagename);
            app_packagename.setText(pi.packageName);

            Drawable icon = pi.applicationInfo.loadIcon(pm);
            myImageView.setImageDrawable(icon);
        }
    }
}






Step 4:
用 Run As執行看看App, 執行畫面如下:



透過 packageInfo 多取得 packageName 屬性:



PackageManager Class Overview


Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().


相關文章:

source code 下載:
http://imaxlive.googlecode.com/files/GetApplicationIconDemo.zip

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...