android - 如何使Android应用程序强制更新?

标签 android google-play updates

有没有一种方法可以在应用程序启动时使用代码检查我在 Google Play 中的应用程序更新并强制应用程序更新?

最佳答案

此代码每天在后台检查一次 Activity 的更新。如果发现更新(比当前版本更高),它会打开一个对话框并要求用户打开市场。

代码有点像这些:-->

http://mycompany.com/update 指向您的服务器站点,您可以在其中放置文本文件或 watever 以获取最新版本并将其与较新版本进行比较。在比较的基础上强制更新

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 - 如何使Android应用程序强制更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14150597/

相关文章:

android - 100 条消息限制适用于一台设备还是 GCM 的每台设备?

android - LVL 拒绝有效许可证是否正常?

heroku - redisClient.set :- no update no error

java - 即时应用程序在发布中无法运行(即时应用程序启动因未知原因失败(getInstantAppPreLaunchInfo 失败))

android - 由于多个 APK 版本,无法在 App Billing V3 中为 Android 生成公钥

c++ - 什么是 Visual Studio 2019 的 cmake 生成器

linux - "libsolv-tools-0.3.2-2.14.1"的 OpenSuse 12.3 更新错误

Android Studio gradle 构建兼容 Android – 5.0 Lollipop 等

android - 使用钛appcelerator中的json将远程mysql数据插入到sqlite中

android - 从 Delphi 调用 JNI_OnLoad