android 正在检查更新,关于在app启动android上检查更新的新手问题

我正在尝试下面的代码.我想在应用程序启动时每天检查一次我的应用程序的更新版本.得到了

http://www.androidsnippets.com/check-for-updates-once-a-day的代码.

我无法弄清楚的是,在行URL updateURL = new URL(“http://my.company.com/update”);什么应该在

http://mycompany.com/update指向?一个xml,txt或..文件.我尝试了一个只有最新版本的xml和txt文件,并将其命名为version.txt,versionCode.txt和.xml.

它只是不起作用,应该只是“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();

}

};

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值