在Android应用程序中运行后台更新

时间:2012-05-29 16:20:06

标签: android

我正在尝试运行一个方法,在后台检查应用程序更新,但我收到以下错误,我想我错过了一些导入的包,但我不是那么肯定。在这方面,我将非常感谢你。

Errors <br>
lastUpdateTime cannot be resolved to a variable
checkUpdate cannot be resolved 
Illegal modifier for the variable checkUpdate; only final is permitted
showUpdate cannot be resolved to a variable
Illegal modifier for the variable showUpdate; only final is permitted

这是我的代码

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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("url to my company");                
                        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) {
                    }
                }
            };

1 个答案:

答案 0 :(得分:0)

我看到的一些问题:

  1. 是lastUpdateTime声明的吗?
  2. checkUpdate正在使用之前 声明。
  3. 在checkUpdate声明中删除private。
  4. 修复这些内容,然后查看是否有更多错误。