我们一般把全局的东西放到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();进行检查版本是否要进行升级.