2011-03-06 62 views
4

我正在尝试下面的代码。假设每天应用程序启动时检查我的应用程序的更新版本。得到http://www.androidsnippets.com/check-for-updates-once-a-day的代码。 我弄不明白的是,在URL URL updateURL = new URL(“http://my.company.com/update”); http://mycompany.com/update指向什么?一个xml,txt或..文件。我用最新版本的代码尝试了一个xml和txt文件,并将其命名为version.txt,versionCode.txt和.xml。关于检查应用程序启动更新的新手问题android

它只是不工作,应该只是“http://mycompany.com/update”或扩展名,即“http://mycompany.com/update/version.txt”或其他东西。

在此先感谢。

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(); 
     } 
};  

}

回答

1
  1. 实际URL并不重要,只要你可以有一个文件从该服务。
  2. 查看代码,文件本身需要是一个文本/纯文本(.txt)文件,其中包含您的应用程序的当前版本代码,而不包含尾随换行符。
+0

Thanks Dre,所以文件名称并不重要?或者我应该在第一行使用android:versionCode =“1”将它命名为versionCode.txt? – cyben 2011-03-06 15:09:22

+0

它必须*只是*数字,不换行。只要您的服务器可以提供服务器,文件名就不重要了。 – Dre 2011-03-06 16:19:34

+0

好的注意谢谢。 – cyben 2011-03-06 16:39:56

4

首先,这是多余的,因为Android Market automaticaly会通知用户更新,但为了做到这一点,您需要一台服务器,在该服务器上放置一个包含信息的txt文件你的代码寻找,在这种情况下,你的代码寻找一个版本代码。该代码是在这里:然后

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

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

你发布的代码做了所有这些,但要更具体地回答你的问题在你需要将URL到你的“currentVersion.txt文件”或什么是文件名是什么。

希望我帮了忙!

+0

Ic,谢谢Aaron的快速回复。它是一个私人应用。我有一个免费的webhosting服务器,我将放置version.txt文件。所以在txt中,我只需要放置android:versionCode =“3”或者仅仅是3.我不会将它链接到市场,当更新可用时它将指向我自己的文件托管网址。将新的Intent(Intent.ACTION_VIEW,Uri.parse(“http://mycompany.com/wherethefileis”));工作? – cyben 2011-03-06 15:08:26

+0

只是3.我没有打开URL和下载文件,所以我不知道那个。我曾与FTP的东西,这可能适合你下载一个完整的新版本的应用程序更好。抱歉。 – 2011-03-06 15:17:14

+0

好吧..打开URL的作品,现在只需要弄清楚如何在下载完成后自动打开文件..谢谢所有 – cyben 2011-03-06 16:39:00

1

我发现这个代码是非常有用的,但需要这样的versionName小的升级可以为(即1.01) 改变的checkUpdate检查:

/* Get current Version Number */ 
String curVersionStr = getPackageManager().getPackageInfo("package id", 0).versionName; 
float curVersion = Float.parseFloat(curVersionStr); 
float newVersion = Float.parseFloat(s); 

希望这可以帮助别人。