应用软件更新一般步骤:
1、首先检测网络是否存在,如果存在进行版本信息检测
[java] view
plaincopy
- NetworkInfo networkInfo = NetInfo.getNetworkInfo(getActivity());
- if (networkInfo != null) {
- state = networkInfo.getState();
- typeName = networkInfo.getTypeName();
- apnName = networkInfo.getExtraInfo();
- if (state == NetworkInfo.State.CONNECTED) {
- if (typeName.equals("MOBILE")) {
- if (apnName.equals("ctnet") || apnName.equals("cmnet") || apnName.equals("internet")) {
- checkUpdate();
- } else {
- setNet();
- }
- } else {
- checkUpdate();
- }
- } else {
- Message message = new Message();
- message.what = 102;
- handler.sendMessage(message);
- }
- } else {
- Message message = new Message();
- message.what = 102;
- handler.sendMessage(message);
- }
2、检测当前版本的信息AndroidManifest.xml-->manifest-->android:versionName,我使用的是
[html] view
plaincopy
- android:versionCode="99"
- android:versionName="3.0.0">
3、从服务器获取版本号(版本号存在于xml文件中)并与当前检测到的版本进行匹配,如果不匹配,提示用户进行升级,如果匹配则进入程序主界面。
[java] view
plaincopy
- private void checkUpdate() {
- //统计用户打开客户端的量
- UserController.commitUserOpenInfo(getActivity());
- //获取服务器端apk版本号
- CommonController.getInstance().getAppVersionCode(getActivity(), versionCodehandler);//获取版本号
- }
4、当提示用户进行版本升级时,如果用户点击了确定,系统将自动从服务器上下载并进行自动升级,如果点击取消将进入程序主界面。
[java] view
plaincopy
- private Handler versionCodehandler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- versionCode = (Integer)msg.obj;//返回的数值为版本号
- if(pinfo.versionCode < versionCode) {
- CommonController.getInstance().getAppUpdates(getActivity(), updateInfohandler);
- }
- System.out.println("versionCode = " + versionCode);
- };
- };
去下载更新
[java] view
plaincopy
- private Handler updateInfohandler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- updateInfo = (String)msg.obj;
- updateInfo = updateInfo.replace("\\n", "\n");
- new AlertDialog.Builder(getActivity()).setTitle("提示").setMessage(updateInfo)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- openURL(DamaiDataAccessApi.APK_DOWNLOAD_ADDRESS);//下载更新
- }
- }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- handler.sendEmptyMessage(0);
- }
- })
- .setCancelable(false)
- .create().show();
- };
- };
更新实现方式
[java] view
plaincopy
- final void openURL(String url) {
- launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
- }
- void launchIntent(Intent intent) {
- if (intent != null) {
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
- try {
- startAnimaActivity.startActivity(intent);
- try {
- //这个是程序中跳转到首页代码,这里加上是为了手机在浏览器页面点击返回的时候能够回到自己的客户端首页
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- AnimationUtil.startFragmentsAnimationWithFadeInFadeOut(ft);
- MainFragment mf = new MainFragment();
- ft.add(R.id.fragmentRoot, mf, FragmentFlagNameList.MAIN);
- ft.addToBackStack(FragmentFlagNameList.MAIN);
- ft.commit();
- } catch (Exception e) {
- // TODO: handle exception
- }
- } catch (ActivityNotFoundException e) {
- }
- }
- }
时间: 2024-11-08 22:17:10