解析apk文件的图标等内容

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;

import eric.test.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity {
	private ImageView img;
	private String archiveFilePath =Environment.getExternalStorageDirectory() + "/"+ "lindong/生活百事通之营养.apk";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		img = (ImageView)findViewById(R.id.img);
		Drawable icon = getUninstallAPKIcon(this, archiveFilePath);
		img.setBackgroundDrawable(icon);
	}
	public static Resources getResources(Context context , String apkPath) throws Exception {  
        String PATH_AssetManager = "android.content.res.AssetManager";        
        Class assetMagCls = Class.forName(PATH_AssetManager);    
        Constructor assetMagCt = assetMagCls.getConstructor((Class[]) null);    
        Object assetMag = assetMagCt.newInstance((Object[]) null);    
        Class[] typeArgs = new Class[1];    
        typeArgs[0] = String.class;    
        Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath",    
                typeArgs);    
        Object[] valueArgs = new Object[1];    
        valueArgs[0] = apkPath;    
        assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);  
        Resources res = context.getResources();    
        typeArgs = new Class[3];    
        typeArgs[0] = assetMag.getClass();    
        typeArgs[1] = res.getDisplayMetrics().getClass();    
        typeArgs[2] = res.getConfiguration().getClass();    
        Constructor resCt = Resources.class.getConstructor(typeArgs);    
        valueArgs = new Object[3];    
        valueArgs[0] = assetMag;    
        valueArgs[1] = res.getDisplayMetrics();    
        valueArgs[2] = res.getConfiguration();    
        res = (Resources) resCt.newInstance(valueArgs);          
        return res;  
    }  
	
	public static Drawable getUninstallAPKIcon(Context context,String apkPath) {    
	    PackageManager pm = context.getPackageManager();    
	        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);          
	        Resources res = null;  
	        try {  
	        res = getResources(context,apkPath);  
	    } catch (Exception e) {  
	        return null;  
	    }  
	    if(info != null){  
	        ApplicationInfo appInfo = info.applicationInfo;  
	        return res.getDrawable(appInfo.icon);   
	    }  
	          
	    return null;          
	    } 
	

}

 其实关键还是获取可以获取apk内容的Resource对象

转自:http://blog.csdn.net/sky181772733/article/details/7220401

相关推荐