重新安装应用程序不会带来已发布的应用程序android的所有更改

时间:2014-08-15 16:13:41

标签: android reinstall

我有一个项目正在重新安装新版本的旧应用程序,我使用自定义自安装程序来安装应用程序。我在重新安装时看到了一些奇怪的行为。当应用程序下载应用程序的已发布版本时,并非所有最新更改都随附。它正在几天前安装一个版本。不知道为什么会这样。

我想我需要在自我安装程序中完全删除并重新安装该应用程序。

以下是自安装程序的代码:

public class AsyncActivity extends Activity {


public static int taskID;

Intent keepInApp;

private boolean messageShowing = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    lblUpdating = (TextView)findViewById(R.id.lblUpdating);

    taskID = getTaskId();

    keepInApp = new Intent(this.getApplicationContext(), ServiceKeepInApp.class);

    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        thepackageName = bundle.getString(GlobalVars.keyPackageName);
        GlobalVars.KeyPackageName = thepackageName;

        urlPath = bundle.getString(GlobalVars.keyFTPPath);
        GlobalVars.KeyFTPPath = urlPath;

        downloadPath = bundle.getString(GlobalVars.keyDownloadLocation);
        GlobalVars.deviceDownloadPath = downloadPath;

        user = bundle.getString(GlobalVars.keyFTPUser);
        GlobalVars.FTPUser = user;

        pw = bundle.getString(GlobalVars.keyFTPPassword);
        GlobalVars.FTPPassword = pw;

        apkName = bundle.getString(GlobalVars.keyFileName);
        GlobalVars.APKName = apkName;

        serverVersion = bundle.getString(GlobalVars.keyServerVersion);
        GlobalVars.ServerVersion = serverVersion;

        if (bundle.getString(GlobalVars.keyScreenText) != null) {
            lblUpdating.setText(Html.fromHtml(bundle.getString(GlobalVars.keyScreenText)));
        }

        if (bundle.getString(GlobalVars.keyFont) != null) {
            if (!bundle.getString(GlobalVars.keyFont).equalsIgnoreCase("")) {
                Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/" + bundle.getString(GlobalVars.keyFont));
                lblUpdating.setTypeface(typeFace);
            }
        }

        if (StringUtils.isBlank(urlPath) || StringUtils.isBlank(downloadPath) || StringUtils.isBlank(user) || StringUtils.isBlank(pw)
                || StringUtils.isBlank(apkName) || StringUtils.isBlank(thepackageName)) {
            stopService(keepInApp);
            finish();
            android.os.Process.killProcess(android.os.Process.myPid());

        } else {
            startService(keepInApp);
        }
    }

    try {
        int position = urlPath.lastIndexOf(".");
        ftpServerName = urlPath.substring(0, position + 4); // +4 so we get .com
        ftpUpdatePath = urlPath.substring(position + 4); // +4 so we don't get .copm

        boolean downloadAPK = true;

        try {
            File apk = new File(downloadPath, apkName);

            if (apk != null) {
                try {
                    PackageManager pm = getPackageManager();
                    PackageInfo pi = pm.getPackageArchiveInfo(downloadPath + apkName, 0);
                    pi.applicationInfo.sourceDir = downloadPath + apkName;
                    pi.applicationInfo.publicSourceDir = downloadPath + apkName;

                    if (Double.valueOf(pi.versionName).equals(Double.valueOf(serverVersion))) {
                        downloadAPK = false;
                        InstallApplication(thepackageName, apkName, downloadPath);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            downloadAPK = false;
            ProgressTask task = (ProgressTask)new ProgressTask(this);
            task.execute(user, pw, ftpServerName, ftpUpdatePath, downloadPath, apkName, thepackageName);
            e.printStackTrace();
        }

        if (downloadAPK) {
            ProgressTask task = (ProgressTask)new ProgressTask(this);
            task.execute(user, pw, ftpServerName, ftpUpdatePath, downloadPath, apkName, thepackageName);
        }

    } catch (Exception e) {
        stopService(keepInApp);
        finish();
        android.os.Process.killProcess(android.os.Process.myPid());
        e.printStackTrace();
    }
}

    public void InstallApplication(String packageName, String apkName, String installPath) {
    setIsMessageShowing(true);

    Uri packageURI = Uri.parse(packageName);
    // Intent intent = new Intent(android.content.Intent.ACTION_VIEW, packageURI);
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, packageURI);

    /*
     * Right here, we should be able to change the relative file-pathing to
     * wherever we choose to download the apk to.
     */

    intent.setDataAndType(Uri.fromFile(new File(installPath.toString() + apkName.toString())), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

对于服务:

public class ServiceKeepInApp extends Service {

private boolean sendHandler = false;

Handler taskHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        ActivityManager activityManager = (ActivityManager)getSystemService(Service.ACTIVITY_SERVICE);

        if (activityManager.getRecentTasks(2, 0).get(0).id != AsyncActivity.taskID) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            Context mycon = getApplicationContext();
            PackageManager manager = mycon.getApplicationContext().getPackageManager();
            intent = manager.getLaunchIntentForPackage(mycon.getPackageName());

            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            intent.putExtra("keyFTPPath", GlobalVars.FTPPath);
            intent.putExtra("keyDownloadLocation", GlobalVars.deviceDownloadPath);
            intent.putExtra("keyFTPUser", GlobalVars.FTPUser);
            intent.putExtra("keyFTPPassword", GlobalVars.FTPPassword);
            intent.putExtra("keyFileName", GlobalVars.APKName);
            intent.putExtra("keyPackageName", GlobalVars.KeyPackageName);
            intent.putExtra(GlobalVars.keyServerVersion, GlobalVars.ServerVersion);

            mycon.startActivity(intent);
        }

        if (sendHandler) {
            taskHandler.sendEmptyMessageDelayed(0, 1000);
        }
    }
};

@Override
public void onCreate() {
    Log.v("Service", "created");
    super.onCreate();
    sendHandler = true;
    taskHandler.sendEmptyMessage(0);
}

以及下载软件的AsyncTask:

    class ProgressTask extends AsyncTask<String, Void, Boolean> {
    List<Message> titles;
    private FTPClient mFTPClient = null;

    ProgressTask(Context asyncActivity) {
        context = asyncActivity;
    }

    /** progress dialog to show user that the backup is processing. */

    /** application context. */
    private Context context;

    protected Boolean doInBackground(final String... args) {
        Boolean status = null;

        try {
            status = ftpConnect(args[2], args[0], args[1], 21);

            if (status) {
                File destinationPath = new File(args[4]);

                if (!destinationPath.exists()) {
                    destinationPath.mkdirs();
                }

                File fromFile = new File(args[3] + args[5]);

                File toFile = new File(args[4] + "/" + args[5]);

                if (toFile.exists()) {
                    toFile.delete();
                }

                status = ftpDownload(fromFile.toString(), toFile.toString());

                mFTPClient.logout();
                mFTPClient.disconnect();

                InstallApplication(args[6], args[5], args[4]);
            }
            return status;

        } catch (Exception e) {
            e.printStackTrace();
            return status;
        }
    }

重新安装后,为什么以前版本的软件会停留在应用程序上?我可以使用以下代码删除旧包:

    public void unInstallApp(String packageName) {
        Uri packageURI = Uri.parse(packageName.toString());
        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
        context.startActivity(uninstallIntent);
    }

1 个答案:

答案 0 :(得分:0)

文件没有被覆盖,所以这就是发布版本没有更新的原因。