适用于简单项目的BaseActivity

适用于简单项目的BaseActivity.随手记录一下.如有用到其他自行添加.

public abstract class BaseActivity extends AppCompatActivity {
    /**
     * 是否禁止旋转屏幕
     **/
    private boolean isAllowScreenRoate = false;
    private SharedPreferences mSharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!isAllowScreenRoate) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        setScreenRoate(false);
        bindView();
    }

    public abstract void bindView();

    public abstract void initView();

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class clz) {
        startActivity(new Intent(BaseActivity.this, clz));
    }

    /**
     * [页面回调跳转]
     *
     * @param clz
     */
    public void startActivityForResult(Class clz, int requestCode) {
        startActivityForResult(new Intent(BaseActivity.this, clz), requestCode);
    }

    /**
     * [携带数据的页面回调跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * [携带数据的页面回调跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class clz, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * [简化Toast]
     *
     * @param msg
     */
    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * [是否允许屏幕旋转]
     *
     * @param isAllowScreenRoate
     */
    public void setScreenRoate(boolean isAllowScreenRoate) {
        this.isAllowScreenRoate = isAllowScreenRoate;
    }

    /**
     * [保存用户信息]
     *
     * @param account  用户名
     * @param password 密码
     * @return 是否成功
     */
    public boolean setUserInfo(String id, String account, String password, String realName) {
        mSharedPreferences = getSharedPreferences(AppConfig.USER_INFO_PREFERENCES, MODE_PRIVATE);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(AppConfig.USER_INFO_ID, id);
        editor.putString(AppConfig.USER_INFO_ACCOUNT, account);
        editor.putString(AppConfig.USER_INFO_PASSWORD, password);
        editor.putString(AppConfig.USER_INFO_REAL_NAME, realName);
        return editor.commit();
    }

    /**
     * 获取用户id
     *
     * @return 用户id
     */
    public String getUserId() {
        mSharedPreferences = getSharedPreferences(AppConfig.USER_INFO_PREFERENCES, MODE_PRIVATE);
        return mSharedPreferences.getString(AppConfig.USER_INFO_ID, "");
    }

    /**
     * 获取用户名
     *
     * @return 用户名
     */
    public String getUserAccount() {
        mSharedPreferences = getSharedPreferences(AppConfig.USER_INFO_PREFERENCES, MODE_PRIVATE);
        return mSharedPreferences.getString(AppConfig.USER_INFO_ACCOUNT, "");
    }

    /**
     * 获取密码
     *
     * @return 密码
     */
    public String getUserPassword() {
        mSharedPreferences = getSharedPreferences(AppConfig.USER_INFO_PREFERENCES, MODE_PRIVATE);
        return mSharedPreferences.getString(AppConfig.USER_INFO_PASSWORD, "");
    }

    /**
     * 获取用户姓名
     *
     * @return 用户姓名
     */
    public String getUserRealName() {
        mSharedPreferences = getSharedPreferences(AppConfig.USER_INFO_PREFERENCES, MODE_PRIVATE);
        return mSharedPreferences.getString(AppConfig.USER_INFO_REAL_NAME, "");
    }

    /**
     * 动态获取ListView高度
     *
     * @param listView listView
     * @return 高度
     */
    public static int getTotalHeightOfListView(ListView listView) {
        ListAdapter mAdapter = listView.getAdapter();
        if (mAdapter == null) {
            return 0;
        }
        int totalHeight = 0;
        for (int i = 0; i < mAdapter.getCount(); i++) {
            View mView = mAdapter.getView(i, null, listView);
            mView.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            //mView.measure(0, 0);
            totalHeight += mView.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
        return totalHeight;
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return this.getString(R.string.can_not_find_version_name);
        }
    }

    /**
     * 版本号比较
     *
     * @param version1 版本号1
     * @param version2 版本号2
     * @return 0 相等,1 v1 > v2,-1 v1 < v2
     */
    public static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");
        int index = 0;
        // 获取最小长度值
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;
        // 循环判断每位的大小
        while (index < minLen && (diff = Integer.parseInt(version1Array[index]) - Integer.parseInt(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            // 如果位数不一致,比较多余位数
            for (int i = index; i < version1Array.length; i++) {
                if (Integer.parseInt(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Integer.parseInt(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

    /**
     * 简单Dialog
     *
     * @param title 标题
     * @param message 内容
     * @param positive 右
     * @param negative 左
     */
    public void showSimpleDialog(String title, String message, String positive, String negative) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        if (title.length() > 0) {
            builder.setTitle(title);
        }
        if (message.length() > 0) {
            builder.setMessage(message);
        }
        if (positive.length() > 0) {
            builder.setPositiveButton(positive, null);
        }
        if (negative.length() > 0) {
            builder.setNegativeButton(negative, null);
        }
        builder.create().show();
    }
}

你可能感兴趣的:(BaseActivity)