0

下面是我的代码,它提供了 SD 卡中的所有应用程序并安装在手机内存中,但我想在我的应用程序内存中安装选定的应用程序,所以当用户卸载应用程序时,用户从该应用程序列表中安装的所有应用程序也将删除我该怎么办?请帮我

           package com.gfsmart.filemanager;


  import android.widget.Button;
  import android.widget.ListView;
  import android.widget.TextView;

     public class SdcardAPkMgr extends ListActivity {
DatabaseHandler db;
private List<FileInformation> files_list;
private ProgressDialog mLoadDialog;
private Handler handler;
private static final int SCAN_APK_START = 101;
private static final int SCAN_APK_COMLETED = 102;
private boolean mJustCreate = true;
public static String yahoo;


/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.apk_list);
    files_list = new ArrayList<FileInformation>();
      db = new DatabaseHandler(this);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    setupBatchHandler();
    if (mJustCreate) {
        thread.start();
    }
    mJustCreate = false;
}

private void setupBatchHandler() {
    // TODO Auto-generated method stub
    handler = new Handler() {
        // @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case SCAN_APK_START: {
                mLoadDialog = 
      ProgressDialog.show(SdcardAPkMgr.this, "",
                        getString(R.string.scan_apk_info));
                mLoadDialog.setCancelable(false);
                break;
            }
            case SCAN_APK_COMLETED: {
                ApkListAdapter adapter = new ApkListAdapter(
                        SdcardAPkMgr.this, files_list);
                SdcardAPkMgr.this.setListAdapter(adapter);
                mLoadDialog.dismiss();
                break;
            }
            default:
                break;
            }
            super.handleMessage(msg);
        }
    };
}

Thread thread = new Thread(new Runnable() {

    public void run() {
        // TODO Auto-generated method stub
        handler.sendEmptyMessage(SCAN_APK_START);
        getAllApkFiles(new File("/sdcard"));
        handler.sendEmptyMessage(SCAN_APK_COMLETED);
    }
});

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    FileInformation information = files_list.get(position);
     Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(information.getPath())), 
  "application/" +
                        "vnd.android.package-archive");
        this.startActivity(intent);
}

public Drawable getAPKDrawable(String filePath) {
    Drawable dr = null;
    if (filePath != null) {

        String PATH_PackageParser = "android.content.pm.PackageParser";
        String PATH_AssetManager = "android.content.res.AssetManager";
        try {
            Class pkgParserCls = Class.forName(PATH_PackageParser);
            Class[] typeArgs = new Class[1];
            typeArgs[0] = String.class;
            Constructor pkgParserCt = 
       pkgParserCls.getConstructor(typeArgs);
            Object[] valueArgs = new Object[1];
            valueArgs[0] = filePath;
            Object pkgParser = pkgParserCt.newInstance(valueArgs);
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.setToDefaults();
            // PackageParser.Package mPkgInfo =
            // packageParser.parsePackage(new
            // File(apkPath), apkPath,
            // metrics, 0);
            typeArgs = new Class[4];
            typeArgs[0] = File.class;
            typeArgs[1] = String.class;
            typeArgs[2] = DisplayMetrics.class;
            typeArgs[3] = Integer.TYPE;
            Method pkgParser_parsePackageMtd = pkgParserCls
                    .getDeclaredMethod("parsePackage", 
           typeArgs);
            valueArgs = new Object[4];
            valueArgs[0] = new File(filePath);
            valueArgs[1] = filePath;
            valueArgs[2] = metrics;
            valueArgs[3] = 0;
            Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(
                    pkgParser, valueArgs);
            Field appInfoFld = 
      pkgParserPkg.getClass().getDeclaredField(
                    "applicationInfo");
            ApplicationInfo info = (ApplicationInfo) appInfoFld 
        .get(pkgParserPkg);

            Class assetMagCls = Class.forName(PATH_AssetManager);
            Constructor assetMagCt = assetMagCls
                    .getConstructor((Class[]) null);
            Object assetMag = assetMagCt.newInstance((Object[]) null);
            typeArgs = new Class[1];
            typeArgs[0] = String.class;
            Method assetMag_addAssetPathMtd = assetMagCls
                    .getDeclaredMethod("addAssetPath", 
          typeArgs);
            valueArgs = new Object[1];
            valueArgs[0] = filePath;
            assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
            Resources res = 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);
            String label = null;
            if (info.labelRes != 0) {
                label = (String) res.getText(info.labelRes);
                Log.d("Insert: ", "Inserting ..");
        //      db.deleteContact();
                db.addContact(new Contact(label));
4

0 回答 0