[Java] Android每天检测一次是否有更新 →→→→→进入此内容的聊天室

来自 , 2019-08-06, 写在 Java, 查看 115 次.
URL http://www.code666.cn/view/671d8d05
  1. public class Test extends Activity {
  2.     private Handler mHandler;
  3.  
  4.     @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.front);
  8.         mHandler = new Handler();
  9.        
  10.         /* Get Last Update Time from Preferences */
  11.         SharedPreferences prefs = getPreferences(0);
  12.         lastUpdateTime =  prefs.getLong("lastUpdateTime", 0);
  13.        
  14.         /* Should Activity Check for Updates Now? */
  15.         if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {
  16.  
  17.             /* Save current timestamp for next Check*/
  18.             lastUpdateTime = System.currentTimeMillis();            
  19.             SharedPreferences.Editor editor = getPreferences(0).edit();
  20.             editor.putLong("lastUpdateTime", lastUpdateTime);
  21.             editor.commit();        
  22.  
  23.             /* Start Update */            
  24.             checkUpdate.start();
  25.         }
  26.     }
  27.    
  28.     /* This Thread checks for Updates in the Background */
  29.     private Thread checkUpdate = new Thread() {
  30.         public void run() {
  31.             try {
  32.                 URL updateURL = new URL("http://my.company.com/update");                
  33.                 URLConnection conn = updateURL.openConnection();
  34.                 InputStream is = conn.getInputStream();
  35.                 BufferedInputStream bis = new BufferedInputStream(is);
  36.                 ByteArrayBuffer baf = new ByteArrayBuffer(50);
  37.                
  38.                 int current = 0;
  39.                 while((current = bis.read()) != -1){
  40.                      baf.append((byte)current);
  41.                 }
  42.  
  43.                 /* Convert the Bytes read to a String. */
  44.                 final String s = new String(baf.toByteArray());        
  45.                
  46.                 /* Get current Version Number */
  47.                 int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
  48.                 int newVersion = Integer.valueOf(s);
  49.                
  50.                 /* Is a higher version than the current already out? */
  51.                 if (newVersion > curVersion) {
  52.                     /* Post a Handler for the UI to pick up and open the Dialog */
  53.                     mHandler.post(showUpdate);
  54.                 }                
  55.             } catch (Exception e) {
  56.             }
  57.         }
  58.     };
  59.  
  60.     /* This Runnable creates a Dialog and asks the user to open the Market */
  61.     private Runnable showUpdate = new Runnable(){
  62.            public void run(){
  63.             new AlertDialog.Builder(Test.this)
  64.             .setIcon(R.drawable.icon)
  65.             .setTitle("Update Available")
  66.             .setMessage("An update for is available!\\n\\nOpen Android Market and see the details?")
  67.             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  68.                     public void onClick(DialogInterface dialog, int whichButton) {
  69.                             /* User clicked OK so do some stuff */
  70.                             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));
  71.                             startActivity(intent);
  72.                     }
  73.             })
  74.             .setNegativeButton("No", new DialogInterface.OnClickListener() {
  75.                     public void onClick(DialogInterface dialog, int whichButton) {
  76.                             /* User clicked Cancel */
  77.                     }
  78.             })
  79.             .show();
  80.            }
  81.     };    
  82. }
  83.  
  84. //java/5502

回复 "Android每天检测一次是否有更新"

这儿你可以回复上面这条便签

captcha