gpt4 book ai didi

android - 关于检查应用程序启动 android 更新的新手问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:42:39 25 4
gpt4 key购买 nike

我正在尝试下面的代码。它应该在应用程序启动时每天检查一次我的应用程序的更新版本。从 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。

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

最佳答案

首先,这有点多余,因为 Android Market 会自动通知用户更新,但是,为了做到这一点,您需要一个服务器,在该服务器上放置一个 txt 文件,其中包含您的代码看起来的信息因为,在这种情况下,您的代码会查找版本代码。该代码在这里:

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

然后,您的应用必须将两者进行比较,如果 newVersion 大于 curVersion,则将市场打开到您的应用页面,用户可以在其中更新它。

您发布的代码完成了所有这些,但要更具体地回答您在 url 中的问题,您需要将 url 放入您的“currentVersion.txt 文件”或任何文件名。

希望我有所帮助!

关于android - 关于检查应用程序启动 android 更新的新手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com