检查android应用程序中不在Google Play上的更新

检查android应用程序中不在Google Play上的更新,android,android-activity,Android,Android Activity,我对android编码非常陌生,所以如果我在问这个问题时犯了一些错误,我很抱歉。 我正在尝试为个人使用的应用程序实现应用程序内更新,而不是放在play store上。我已经在堆栈中找到了各种方法,通过这些方法可以实现这一点。所以我尝试了我自己的。但问题是,它有时不起作用,有时它会显示有关可用更新的错误信息。更具体地说,当我将curVersionCode与newVersionCode(其中curVersionCode=newVersionCode)进行比较时,它仍然显示新版本可用 这是我的活动-

我对android编码非常陌生,所以如果我在问这个问题时犯了一些错误,我很抱歉。 我正在尝试为个人使用的应用程序实现应用程序内更新,而不是放在play store上。我已经在堆栈中找到了各种方法,通过这些方法可以实现这一点。所以我尝试了我自己的。但问题是,它有时不起作用,有时它会显示有关可用更新的错误信息。更具体地说,当我将curVersionCode与newVersionCode(其中curVersionCode=newVersionCode)进行比较时,它仍然显示新版本可用

这是我的活动-

public class Test extends Activity {
    private Handler mHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.front);
        mHandler = new Handler();
        checkUpdate.start();
        }
    }

    /* This Thread checks for Updates in the Background */
    private Thread checkUpdate = new Thread() {
        public void run() {
            try {
                URL updateURL = new URL("URL TO THE TXT FILE");
                BufferedReader in = new BufferedReader(new InputStreamReader(updateURL.openStream()));
                String str;
                while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
                /* Get current Version Number */
                PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
                int curVersion = packageInfo.versionCode;
                int newVersion = Integer.valueOf(str);

                /* Is a higher version than the current already out? */
                if (newVersion > curVersion) {
                    /* Post a Handler for the UI to pick up and open the Dialog */
                    mHandler.post(showUpdate);
                }

                }
                in.close();
            } catch (Exception e) {
            }
        }

    };

    /* This Runnable creates a Dialog and asks the user to open the Update Link*/
    private Runnable showUpdate = new Runnable(){
        public void run(){
            new AlertDialog.Builder(BrowserActivity.this)
                    .setIcon(R.drawable.ic_launcher)
                    .setTitle("Update Available")
                    .setMessage("An update for is available!\n\nOpen Update page and see the details?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            /* User clicked OK so do some stuff */
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL TO THE UPDATE FILE"));
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            /* User clicked Cancel */
                        }
                    })
                    .show();
        }
    };
已经搜索了几天,仍然找不到答案。如果这是一个基本问题,我很抱歉,但我对这个问题还很陌生

谢谢


编辑:代码现在运行正常。事实证明,通过internet文本文件刷新版本代码需要一些时间。没关系,我有经验

是否检查“int newVersion”的值??可能是在您的服务中,它没有返回正确的value@Sree它提供了正确的版本,例如,如果TXT文件中的版本为27,则它将27显示为newVersionCode。此外,如果curVersionCode为27,则表明有可用的更新。那么问题出在哪里?@Sree好的,只有当newVersionCode>curVersionCode时才会显示更新,但即使当newVersionCode==curVersionCode时,更新也会显示