本文介绍了Android应用自动更新,如果再APK present删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试(没有成功),让我大的APK更新,从一个自定义的Java应用程序安装(通过ADB),尽管计算器和几个实验的帮助,路线似乎总是失败(请参阅安装APK)

I have tried (without success) to get my large apk updates to install (via adb) from a custom java app, despite the help from stackoverflow and several experiments that route always seems to fail (see Java Application to install APK on android).

它安装在应用程序和设备处于脱机状态,而不是只发布到市场。

The app and devices it is installed on are offline only and not published to the market.

我决定尝试同样的问题,不同的路线;我可以从Java应用程序推到APK /sdcard/MyApp/updates/update.apk

I have decided to try a different route to the same problem; I can push the apk from the java app to /sdcard/MyApp/updates/update.apk

当用户运行MYAPP它来检查update.apk的presence,如果它是present运行更新的myapp我想。当更新完成后,我想update.apk(每个应用程序启动时为prevent的更新循环)被删除。我是新来的Andr​​oid和真的不知道如何实施上述行为。

I would like when the user runs myapp it to check the presence of update.apk and if it is present run the update to myapp. When the update is complete I would like update.apk to be deleted (to prevent an update loop each time the app starts). I am new to android and am not really sure how to implement the above behaviour.

我的code是pretty稀疏的功能块,但低于包括给一个想法,我在想什么:

My code is pretty sparse "chunks" of functionality but included below to give an idea what I am thinking:

if (update.exists()) {
    try {

      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/MyApp/updates" + "updates.apk")), "application/vnd.android.package-archive");
      startActivity(intent); 

}
 //add a delete to update.apk here AFTER it has finished installing
}

我的问题是:

有没有执行上述所需的功能更好的办法?
我如何才能确保update.apk安装并删除之前的工作?

Is there a better way to implement the above required functionality?How can I be sure the update.apk has installed and worked before deleting it?

感谢您的帮助,正如我所说,我是新来的Java和Android,并试图通过战斗

Thanks for the help, as I mentioned I am new to Java and android and trying to battle through.

编辑:最终的解决方案我使用的:

Final solution I am using:

if (updateTxt.exists()) {

            try {
                BufferedReader br = new BufferedReader(
                        new FileReader(updateTxt));
                String line;

                while ((line = br.readLine()) != null) {
                    String line2[] = line.split(" "); // split the string to get
                                                        // the
                                                        // progress
                    myUpdateVersion = Integer.parseInt(line2[0]); // [0] is the
                                                                    // value we
                                                                    // are
                                                                    // interested
                                                                    // in
                                                                    // so set
                                                                    // it.
                }

            } catch (IOException ex) {
                return;
            }
        } else {
            // no update so do nothing
        }

        if (updateApk.exists()) {
            // updateIntent();
            // now check the version of the update file to see if it can be
            // deleted
            PackageManager packageManager = getPackageManager();
            PackageInfo apkPackageInfo = packageManager.getPackageInfo(
                    "com.myapp.myapp", 0);
            if (apkPackageInfo != null) {
                if (apkPackageInfo.versionCode == myUpdateVersion) {
                    // Update has been installed. Delete update APK
                    updateApk.delete();
                } else {
                    // Update needs to be installed
                    updateIntent();
                }
            } else {
                // no update so do nothing
            }
        }

    } // end updateApk

    public void updateIntent() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                        + "/updates/update.apk")),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }

刘德华

推荐答案

你正在服用的方法是好的,效果很好。您需要了解您现有的应用程序将被关闭(杀死),以执行更新。用户将需要手动返回到您的应用程序。

The approach you are taking is fine, and works well. You need to understand that your existing app will be shutdown (killed) in order to perform the update. The user will need to return to your app manually.

为了删除APK避免无限循环,你需要知道更新的版本号。如果你知道(也许你把它作为文件名,或其他方式的一部分),可以对正在运行的应用版本进行比较。如果它们是一样的,你可以肯定的是你的更新已经安装,您可以删除更新APK。

In order to delete the APK to avoid the infinite loop, you'll need to know the version number of the update. If you know that (maybe you have it as part of the filename, or some other way), you can compare it against the version of your app that is running. If they are the same, you can be sure that your update has been installed and you can delete the update APK.

要确定运行的版本,可以使用以下内容:

To determine the version that is running, you can use the following:

    PackageManager packageManager = getPackageManager();
    PackageInfo apkPackageInfo = packageManager.getPackageInfo("your.package.name", 0);
    if (apkPackageInfo != null) {
        if (apkPackageInfo.versionCode == myUpdateVersion) {
            // Update has been installed. Delete update APK
        } else {
            // Update needs to be installed
    }

这篇关于Android应用自动更新,如果再APK present删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 13:18