在检查新手问题的应用程序启动时的Andr​​oid更新 [英] Newbie question on check for updates on app startup android

查看:90
本文介绍了在检查新手问题的应用程序启动时的Andr​​oid更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的code。它的假设检查更新我的应用程序的版本,每日一次应用程序启动时。从 http://www.androidsnippets.com/得到了code检查换更新一次-A-日。 我想不通的是,在该行的网址器updateURL =新的URL(http://my.company.com/update);什么如若 http://mycompany.com/update 点?一个XML,TXT或..文件。我想一个xml,并与一个最新的版本code txt文件,并把它命名的version.txt,版本code.txt和.xml。

I'm trying the below code. It's suppose to check for newer version of my app once a day when the app starts. Got the code from http://www.androidsnippets.com/check-for-updates-once-a-day. The thing i can't figure out is, in the line URL updateURL = new URL("http://my.company.com/update"); whats should the http://mycompany.com/update point to? An xml, txt or .. file. I tried an xml and txt file with just the latest versionCode and named it version.txt, versionCode.txt and .xml.

这是行不通的,应该在URL只是http://mycompany.com/update或扩展即http://mycompany.com/update/version.txt或别的东西。

It just doesn't work and should the url be just "http://mycompany.com/update" or with extension ie, "http://mycompany.com/update/version.txt" or something else.

在此先感谢。

public class Test extends Activity {
private Handler mHandler;

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

    /* Get Last Update Time from Preferences */
    SharedPreferences prefs = getPreferences(0);
    lastUpdateTime =  prefs.getLong("lastUpdateTime", 0);

    /* Should Activity Check for Updates Now? */
    if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {

        /* Save current timestamp for next Check*/
        lastUpdateTime = System.currentTimeMillis();            
        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putLong("lastUpdateTime", lastUpdateTime);
        editor.commit();        

        /* Start Update */            
        checkUpdate.start();
    }
}

/* This Thread checks for Updates in the Background */
private Thread checkUpdate = new Thread() {
    public void run() {
        try {
            URL updateURL = new URL("http://my.company.com/update");                
            URLConnection conn = updateURL.openConnection(); 
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;
            while((current = bis.read()) != -1){
                 baf.append((byte)current);
            }

            /* Convert the Bytes read to a String. */
            final String s = new String(baf.toByteArray());         

            /* Get current Version Number */
            int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
            int newVersion = Integer.valueOf(s);

            /* 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);
            }                
        } catch (Exception e) {
        }
    }
};

/* This Runnable creates a Dialog and asks the user to open the Market */ 
private Runnable showUpdate = new Runnable(){
       public void run(){
        new AlertDialog.Builder(Test.this)
        .setIcon(R.drawable.icon)
        .setTitle("Update Available")
        .setMessage("An update for is available!\\n\\nOpen Android Market 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("market://search?q=pname:your.app.id"));
                        startActivity(intent);
                }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked Cancel */
                }
        })
        .show();
       }
};    

}

推荐答案

首先,这是那种多余的,因为在Android Market automaticaly通知更新的用户,但是,为了做到这一点,你需要一台服务器,把一个txt文件,其中包含您的code查找,信息在这种情况下,您的code查找一个版本code该服务器上。这code是在这里:

First off, this is sort of redundant as the Android Market automaticaly notifies the user of an update, but, in order to do this you will need a server, place a txt file on that server that contains the information your code looks for, in this case your code looks for a version code. That code is here:

int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
int newVersion = Integer.valueOf(s);

那么

您的应用程序将不得不康普艾两人在一起,如果NEWVERSION大于curVersion打开市场,您的应用程序的页面,用户可以对其进行更新。

Your app will then have to compair the two together and if the newVersion is greater than the curVersion open the market to the page of your app where the user can update it.

您发布做了这一切,但更具体的回答你的问题,你需要把URL发送给currentVersion.txt文件或什么都该文件的名称是URL中的code。

The code you posted did all this, but to more specifically answer your question in the url you need to put the url to your "currentVersion.txt file" or what ever the file name is.

希望我帮助!

这篇关于在检查新手问题的应用程序启动时的Andr​​oid更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆