2016-12-23 73 views
0

我正在研究一个应用程序我想给应用程序用户强制更新,如果在Play商店中有新版本可用,应用程序应该向用户显示一个对话消息。如果新版本可用,如何在Android应用程序中强制更新?

+2

当您打开您的应用程序,然后调用您的服务器上的Web服务,并检查应用程序是否更新?如果应用程序更新,然后在谷歌Play商店中打开您的应用程序并更新您的应用程序。 –

+1

你不能这样做,如果你真的想这样做,那么将版本名称存储在你的服务器中,并且每当用户在Splash屏幕上打开应用程序时说,检查存储在你的服务器中的版本,并检查应用程序安装的版本,基于那个做你的事情。 –

+1

您可以使用外部API检查已安装的应用程序版本和最新版本api。检查这个http://stackoverflow.com/questions/25201349/programmatically-check-play-store-for-app-updates – Praveen

回答

4
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 name="youAreNotUpdatedTitle">Update Available</string> 
    <string name="youAreNotUpdatedMessage">A new version of YOUR_APP_NAME is available. Please update to version\s</string> 
    <string name="youAreNotUpdatedMessage1">\s now</string> 
    <string name="update">Update</string> 

记住,您必须在对话框代码中定义对话框的样式。

现在只需在您的基本活动中写入forceUpdate()函数并在onResume()方法内调用它即可完成!

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

嘿这个代码是为我工作,但不工作在realse prograud启用plz帮助我 –

0

在服务器端存储您的应用程序的versionCode(您在playstore上发布)。每次用户打开应用程序并获取版本代码时,请打开API。比较应用用户当前正在使用的versionCode和您存储在服务器上的版本。下面是代码来获取的versionCode您的应用程序

PackageManager manager = this.getPackageManager(); 
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0); 
String versionCode = info.versionCode; 

的如果的versionCode不匹配(即从的versionCode服务器>应用程序的versionCode),提示用户更新。

P.S如果您想使用此方法,则每次更新Playstore上的应用程序时都必须更新服务器上的versionCode。

0

在您的第一个活动中,您可以创建一个应该返回最新版本应用的api调用。如果当前版本较低,则将其与当前应用程序版本进行比较,显示一个要求更新的对话框他们更新按钮,可以在string.xml你可以添加你想要这样无论按摩Play商店

0

您可以执行以下操作。

  1. 你应该在你的应用程序,它可以 实施check for the current version of your app on play store此功能。 并且如果用户正在使用旧版本,则提示他们对话框更新为 。
  2. 您的应用程序应该具有任何分析功能(Facebook,Firebase, Localytics等)支持应用程序内消息的SDK集成。与 的帮助,你可以播放推送通知或应用内消息 来更新应用程序。

在这种技术的帮助下,您可以要求您的用户更新应用程序。

相关问题