Android 自动检测版本升级(2)

我们一般把全局的东西放到application里面 

public class MainActivity extends Activity {       private MyApplication myApplication;          @Override       public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);                      checkVersion();       }          /***       * 检查是否更新版本       */       public void checkVersion() {           myApplication = (MyApplication) getApplication();           if (myApplication.localVersion < myApplication.serverVersion) {                  // 发现新版本,提示用户更新                AlertDialog.Builder alert = new AlertDialog.Builder(this);               alert.setTitle("软件升级")                       .setMessage("发现新版本,建议立即更新使用.")                       .setPositiveButton("更新",                               new DialogInterface.OnClickListener() {                                   public void onClick(DialogInterface dialog,                                           int which) {                                       Intent updateIntent = new Intent(                                               MainActivity.this,                                               UpdateService.class);                                       updateIntent.putExtra(                                               "app_name",                                               getResources().getString(                                                       R.string.app_name));                                       startService(updateIntent);                                   }                               })                       .setNegativeButton("取消",                               new DialogInterface.OnClickListener() {                                   public void onClick(DialogInterface dialog,                                           int which) {                                       dialog.dismiss();                                   }                               });               alert.create().show();              }       }   }  

我们在运行应用的时候要checkVersion();进行检查版本是否要进行升级.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/4ef8c312aa39bf1bc11efbaa738e45b7.html