Android根據(jù)包名取得指定程序包的信息(名稱(chēng)、圖標(biāo)……)
本文討論如何根據(jù)包名取得程序包相關(guān)信息,本例演示取得Android系統(tǒng)程序的名稱(chēng)和圖標(biāo)(注意:并不是每一個(gè)程序包都有程序名稱(chēng)和圖標(biāo))。
主程序源碼如下:
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布局文件源碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/mainLayout" > </LinearLayout>
AndroidManifest.xml文件源碼如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas./apk/res/android" package="com.plter.getsystemicon" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".P0001_GetSystemIconActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>