如何比较上传的服务器文件版本与Android市场上的应用程序版本

时间:2012-05-30 12:26:32

标签: android android-intent

我正在尝试检查并在24小时后更新Android应用程序,每当用户启动我的应用程序时,我已编写此代码,但它无法正常工作,它没有显示任何可用更新的消息。< / p>

我已经在服务器上传了一个txt文件,我刚刚写了3作为版本号,我命名了这个文件版本,这个文件的路径是www.mywebsite.com/upload。 version.txt文件存在于上传目录中。 当我运行此应用程序时,我没有收到任何可用于模拟器中的更新的对话消息。请帮我找出问题所在。我是Android开发的新手。 提前谢谢。

package com.test;

import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import java.io.InputStream;
import java.io.BufferedInputStream;
import android.net.Uri;



public class TestActivity extends Activity {
    private Handler mHandler;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mHandler = new Handler();

        SharedPreferences prefs = getPreferences(0);
        long 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();        

            Thread checkUpdate = null;
            /* Start Update */            
            checkUpdate.start();

            /* This Thread checks for Updates in the Background */
            Thread checkUpdatee = new Thread() {
                public void run() {
                    try {
                        URL updateURL = new URL("http://mywebsite.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("com.test", 0).versionCode;
                        int newVersion = Integer.valueOf(s);

                        /* Is a higher version than the current already out? */
                        if (newVersion > curVersion) {
                            Runnable showUpdate = null;
                            /* 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 */ 
            Runnable showUpdate = new Runnable(){
                   public void run(){
                    new AlertDialog.Builder(TestActivity.this)

                    .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:com.test"));
                                    startActivity(intent);
                            }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                    /* User clicked Cancel */
                            }
                    })
                    .show();
                   }
            };    


        }




    }
}

0 个答案:

没有答案