gpt4 book ai didi

android - 在 android 中关闭自动更新时,以编程方式从 Google 商店更新应用程序

转载 作者:行者123 更新时间:2023-11-29 15:50:22 26 4
gpt4 key购买 nike

我在应用程序中面临一个挑战,即我们的客户群始终关闭应用程序功能的“自动更新”。所以他们甚至没有在他们的设备上获得我的应用程序的更新,尽管它在谷歌播放中。我想以编程方式检查,如果我的应用程序的任何新版本正在上传到 google play,那么用户将收到类似“google play 上有新版本,立即更新”的提醒,当用户点击“确定”时,新版本将安装在设备上。

如果您为此共享代码 fragment ,那就太好了。

最佳答案

您可以手动检查更新并通知用户。尝试以下操作:

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

Source

关于android - 在 android 中关闭自动更新时,以编程方式从 Google 商店更新应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474040/

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