前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >羊皮书APP(Android版)开发系列(三)APP引导页启动控制类

羊皮书APP(Android版)开发系列(三)APP引导页启动控制类

作者头像
热心的程序员
发布2018-08-30 10:41:09
5590
发布2018-08-30 10:41:09
举报
文章被收录于专栏:编程之路编程之路

基本上一个完整的APP都会有一个引导页,在APP首次安装或APP更新后第一次打开时显示,这个逻辑是很通用的,所以写成一个工具类,方便使用。

  • APP启动页逻辑有三种情况:
  1. 当APP被首次安装后打开时显示引导页。
  2. 当APP更新版本后,第一次打开时显示引导页。
  3. 当APP再次启动时,跳过引导页。
  • 工具类AppIntroUtil.java代码如下:
代码语言:javascript
复制
package cn.studyou.parchment.utils;
import android.content.Context;
import android.text.TextUtils;
/**
 * 基本功能:app启动引导页控制
 * 创建:王杰
 * 创建时间:16/3/7
 * 邮箱:w489657152@gmail.com
 */
public class AppIntroUtil {
    public static final int LMODE_NEW_INSTALL = 1; //再次启动
    public static final int LMODE_UPDATE = 2;//更新后第一次启动
    public static final int LMODE_AGAIN = 3;//首次安装启动
    public static final String SHARENAME= "lastVersion";
    private boolean isOpenMarked = false;
    private int launchMode = LMODE_AGAIN; //启动-模式
    private static AppIntroUtil instance;
    public static AppIntroUtil getThis() {
        if (instance == null)
            instance = new AppIntroUtil();
        return instance;
    }

    /**
     * 标记-打开app,用于产生-是否首次打开
     * @param context
     */
    public void markOpenApp(Context context) {
        // 防止-重复调用
        if (isOpenMarked)
            return;
        isOpenMarked = true;
        String lastVersion = OperatingSharedPreferences.getString(context,SHARENAME,SHARENAME);
        String thisVersion = VersionUtil.getVersion(context);
        // 首次启动
        if (TextUtils.isEmpty(lastVersion)) {
            launchMode = LMODE_NEW_INSTALL;
            OperatingSharedPreferences.setString(context,SHARENAME,SHARENAME,thisVersion);
        }
        // 更新
        else if (VersionUtil.compareVersion(lastVersion,thisVersion)) {
            launchMode = LMODE_UPDATE;
            OperatingSharedPreferences.setString(context, SHARENAME, SHARENAME, thisVersion);
        }
        // 二次启动(版本未变)
        else
            launchMode = LMODE_AGAIN;
    }
    public int getLaunchMode() {
        return launchMode;
    }
    /**
     * 首次打开,新安装、覆盖(版本号不同)
     * @return
     */
    public boolean isFirstOpen() {
        return launchMode != LMODE_AGAIN;
    }
}
  • AppIntroUtil.java中使用了另外一个工具类OperatingSharedPreferences.java,这个工具类就是对SharedPreferences进行的一个封装,方便使用,代码如下:
代码语言:javascript
复制
package cn.studyou.parchment.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 基本功能:存储和访问SharedPreferences
 * 创建:王杰
 * 创建时间:16/3/7
 * 邮箱:w489657152@gmail.com
 */
public class OperatingSharedPreferences {

    /**
     * 
     * 基本功能:保存String类型数据到SharedPreferences
     * 编写:王杰
     *
     * @param context
     * @param name
     * @param key
     * @param value  
     */

    public static void setString(Context context,
                                 String name, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();// 获取编辑器
        editor.putString(key, value);
        editor.commit();// 提交修改
    }


    /**
     * 
     * 基本功能:取得SharedPreferences中存储的String类型数据
     * 编写:王杰
     *
     * @param context
     * @param name
     * @param key
     * @return 
     */
    public static String getString(Context context,
                                   String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                name, Context.MODE_PRIVATE);
        String signature = sharedPreferences.getString(key, "");
        return signature;
    }

    /**
     * 
     * 基本功能:存储的Int类型数据到SharedPreferences
     * 编写:王杰
     *
     * @param context
     * @param name
     * @param key
     * @return 
     */
    public static void setInt(Context context,
                              String name, String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();// 获取编辑器
        editor.putInt(key, value);
        editor.commit();// 提交修改
    }

    /**
     * 
     * 基本功能:取得SharedPreferences中存储的Int类型数据
     * 编写:王杰
     *
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static int getInt(Context context,
                             String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                name, Context.MODE_PRIVATE);
        int signature = sharedPreferences.getInt(key, 0);
        return signature;
    }
}
  • AppIntroUtil.java还使用了一个工具类VersionUtil.java,是用来比较APP版本。简要说明一下:使用这个工具类,我们是根据APP的versionName来进行判断的,规定versionName名字不少于三位(如:1.0.0,1.1.2),compareVersion方法用来判断两个版本的大小,只比较前两位,若compareVersion方法返回true,则表示有新版本,否则为同一版本或低于当前版本。代码如下:
代码语言:javascript
复制
package cn.studyou.parchment.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * 基本功能:app版本号
 * 创建:王杰
 * 创建时间:16/3/7
 * 邮箱:w489657152@gmail.com
 */
public class VersionUtil {
    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 版本比较
     *
     * @param nowVersion    app版本
     * @param serverVersion 服务器版本
     * @return
     */
    public static boolean compareVersion(String nowVersion, String serverVersion) {

        if (nowVersion != null && serverVersion != null) {
            String[] nowVersions = nowVersion.split("\\.");
            String[] serverVersions = serverVersion.split("\\.");
            if (nowVersions != null && serverVersions != null && nowVersions.length > 1 && serverVersions.length > 1) {
                int nowVersionsFirst = Integer.parseInt(nowVersions[0]);
                int serverVersionFirst = Integer.parseInt(serverVersions[0]);
                int nowVersionsSecond = Integer.parseInt(nowVersions[1]);
                int serverVersionSecond = Integer.parseInt(serverVersions[1]);
                if (nowVersionsFirst < serverVersionFirst) {
                    return true;
                } else if (nowVersionsFirst == serverVersionFirst && nowVersionsSecond < serverVersionSecond) {
                    return true;
                }
            }
        }
        return false;
    }
}
  • 那么R如何使用这个启动控制类AppIntroUtil.java呢?
  1. 在Application类中初始化,我的Application 类名字是ParchmentApplication。
代码语言:javascript
复制
package cn.studyou.parchment;
import android.app.Application;
import cn.studyou.parchment.log.L;
import cn.studyou.parchment.utils.AppIntroUtil;

/**
 * 基本功能:Application
 * 创建:王杰
 * 创建时间:16/3/7
 * 邮箱:w489657152@gmail.com
 */
public class ParchmentApplication extends Application {
    private static ParchmentApplication instance;
    public static final boolean DEVELOPER_MODE = true;

    public static ParchmentApplication getThis() {
        if (instance == null)
            instance = new ParchmentApplication();
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化日志工具
        L.init(DEVELOPER_MODE);
        AppIntroUtil.getThis().markOpenApp(getApplicationContext());
    }
}
  1. 在APP启动控制Activity中使用:
代码语言:javascript
复制
package cn.studyou.parchment.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import cn.studyou.parchment.R;
import cn.studyou.parchment.log.L;
import cn.studyou.parchment.utils.AppIntroUtil;

public class StartUpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_up);
        int launchMode = AppIntroUtil.getThis().getLaunchMode();
        //自己的控制逻辑
        if(launchMode == 3){
            L.e("Again start!");
        }else if(launchMode == 2){
            L.e("Update start!");
        }else if(launchMode == 1){
            L.e("First start!");
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.03.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档