Android根据包名取得指定程序包的信息(名称、图标……)

Android根据包名取得指定程序包的信息(名称、图标……)



本文讨论如何根据包名取得程序包相关信息,本例演示取得Android系统程序的名称和图标(注意:并不是每一个程序包都有程序名称和图标)。

效果图如下:

主程序源码如下:

package com.plter.getsystemicon;
 
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class P0001_GetSystemIconActivity extends Activity {
 
    private ImageView appIconIv;
    private TextView appNameTv;
    private LinearLayout.LayoutParams llp=new LinearLayout.LayoutParams(-2, -2);
    private LinearLayout mainLayout;
 
    private PackageManager pm;
    private ApplicationInfo appInfo;
    private Drawable appIcon;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        mainLayout=(LinearLayout) findViewById(R.id.mainLayout);
 
        appNameTv=new TextView(this);
        mainLayout.addView(appNameTv, llp);
 
        appIconIv=new ImageView(this);
        mainLayout.addView(appIconIv, llp);
 
        pm=getPackageManager();
        try {
            appInfo=pm.getApplicationInfo("system", PackageManager.GET_META_DATA);
 
            appNameTv.setText(pm.getApplicationLabel(appInfo));
 
            appIcon=pm.getApplicationIcon(appInfo);
            appIconIv.setImageDrawable(appIcon);
 
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

main.xml布局文件源码如下:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainLayout"
    >


AndroidManifest.xml文件源码如下:


      package="com.plter.getsystemicon"
      android:versionCode="1"
      android:versionName="1.0">
   
 
   
                          android:label="@string/app_name">
           
               
               
           

       

 
   





你可能感兴趣的:(android,进阶)