本文介紹了如果有新版本可用,如何在 Android 應(yīng)用程序中強(qiáng)制更新?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在開發(fā)一個應(yīng)用程序,如果 Play 商店有新版本可用,我想向應(yīng)用程序用戶強(qiáng)制更新,該應(yīng)用程序應(yīng)該向用戶顯示一條對話框消息.
I am working on an application I want to give force update to app users if new version available on play store, the app should show a dialog message to user.
推薦答案
public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject>{
private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdateAsync(String currentVersion, Context context){
this.currentVersion = currentVersion;
this.context = context;
}
@Override
protected JSONObject doInBackground(String... params) {
try {
latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.getPackageName()+"&hl=en")
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get()
.select("div[itemprop=softwareVersion]")
.first()
.ownText();
} catch (IOException e) {
e.printStackTrace();
}
return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(latestVersion!=null){
if(!currentVersion.equalsIgnoreCase(latestVersion)){
// Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
if(!(context instanceof SplashActivity)) {
if(!((Activity)context).isFinishing()){
showForceUpdateDialog();
}
}
}
}
super.onPostExecute(jsonObject);
}
public void showForceUpdateDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
R.style.DialogDark));
alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
dialog.cancel();
}
});
alertDialogBuilder.show();
}
}
在 string.xml 中你可以像這樣添加任何你想要的按摩.
in string.xml you can add whatever massage you want like this.
<string name="youAreNotUpdatedTitle">Update Available</string>
<string name="youAreNotUpdatedMessage">A new version of YOUR_APP_NAME is available. Please update to versions</string>
<string name="youAreNotUpdatedMessage1">s now</string>
<string name="update">Update</string>
請記住,您必須在對話框代碼中定義對話框的樣式.
remember you have to define the style of your dialog in the dialog code.
現(xiàn)在只需在你的基礎(chǔ)活動中編寫 forceUpdate() 函數(shù)并在 onResume() 方法中調(diào)用它,你就完成了!
now just write the forceUpdate() function in your base activity and call it inside onResume() method and you are done!!
// check version on play store and force update
public void forceUpdate(){
PackageManager packageManager = this.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = packageManager.getPackageInfo(getPackageName(),0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String currentVersion = packageInfo.versionName;
new ForceUpdateAsync(currentVersion,BaseActivity.this).execute();
}
這篇關(guān)于如果有新版本可用,如何在 Android 應(yīng)用程序中強(qiáng)制更新?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!