问题
I have a project that is re-installing a new version of an old app and I use a custom self installer to install the app. I am seeing some strange behavior with the re-install. When the app downloads the released version of the app, not all of the latest changes come with it. It's installing a release from several days ago. Not sure why this is happening.
I am thinking that I need to completely delete and reinstall the app in my self installer program.
Here is the code for the self installer:
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);
}
And for the service:
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);
}
and for the AsyncTask that downloads the software:
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;
}
}
Why would a previous version of the software stay on the app after reinstall? Could I delete the old package with code like:
public void unInstallApp(String packageName) {
Uri packageURI = Uri.parse(packageName.toString());
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
context.startActivity(uninstallIntent);
}
回答1:
File was not being overwritten, so this is why the release version didn't update.
来源:https://stackoverflow.com/questions/25330096/reinstalling-app-does-not-bring-over-all-the-changes-of-released-app-android