2014-03-28 37 views
0

我正在开发一个Android应用程序,并且我被要求做一件奇怪的事情。我的客户希望我强制更新最新版本的应用程序,而不会向用户显示可用更新,并且不会让他按任何更新按钮并前往Google Play商店下载应用程序。这一切都必须无缝完成,无需用户做任何更新他的应用程序。现在我看到了强制更新应用程序的问题,但他们都告诉我们需要重定向到Google Play商店,并且用户需要进行一些互动。尽管我正在寻找的是无需用户交互即可更新应用程序的无缝方式。因此,只要有新版本可用,只要清单文件中的权限没有变化,它就会开始下载它。虽然我确定下载通知将显示在通知栏中。如何实现这一目标?在后台强制更新Android应用程序的最新版本,无需用户按任何按钮

+0

我敢肯定,这是不可能的。您可以在后台下载.apk文件,但用户必须确认其安装。 –

+0

您的客户是否有机会位于俄罗斯?他专注于“有针对性的电子直销”吗? –

+0

嗨Sir 感谢您的回复。但我在这里有一个问题。当我们更改谷歌播放的自动更新设置,然后它下载最新版本,用户不必做任何事情。我想要的东西非常相似...唯一的事情是应用程序应该更新,即使自动更新没有设置为true ..所以我正在寻找一些方法来通过代码..需要你的建议,如果它的可行 –

回答

0

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

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(); 
      } 
    }; 
} 
相关问题