2

每当用户启动我的应用程序时,我都会尝试在 24 小时后检查和更新 Android 应用程序,我已经编写了此代码,但它不起作用,它没有显示任何可用更新的消息。

我在服务器上上传了一个 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();
                   }
            };    


        }




    }
}
4

0 回答 0