Created
March 17, 2019 12:59
-
-
Save mikeyang01/7f9bb451555c0f8805bb3965ef2e114c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//链接:https://www.zhihu.com/question/47045239/answer/105086885 | |
public abstract class BaseActivity extends FragmentActivity implements | |
OnClickListener { | |
/** 是否沉浸状态栏 **/ | |
private boolean isSetStatusBar = true; | |
/** 是否允许全屏 **/ | |
private boolean mAllowFullScreen = true; | |
/** 是否禁止旋转屏幕 **/ | |
private boolean isAllowScreenRoate = false; | |
/** 当前Activity渲染的视图View **/ | |
private View mContextView = null; | |
/** 是否输出日志信息 **/ | |
private boolean isDebug; | |
private String APP_NAME; | |
protected final String TAG = this.getClass().getSimpleName(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
isDebug = MApplication.isDebug; | |
APP_NAME = MApplication.APP_NAME; | |
$Log(TAG + "-->onCreate()"); | |
try { | |
Bundle bundle = getIntent().getExtras(); | |
initParms(bundle); | |
mContextView = LayoutInflater.from(this) | |
.inflate(bindLayout(), null); | |
if (mAllowFullScreen) { | |
this.getWindow().setFlags( | |
WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
} | |
if (isSetStatusBar) { | |
steepStatusBar(); | |
} | |
setContentView(mContextView); | |
if (!isAllowScreenRoate) { | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | |
} else { | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
} | |
initView(mContextView); | |
doBusiness(this); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* [沉浸状态栏] | |
*/ | |
private void steepStatusBar() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
// 透明状态栏 | |
getWindow().addFlags( | |
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | |
// 透明导航栏 | |
getWindow().addFlags( | |
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); | |
} | |
} | |
/** | |
* [初始化Bundle参数] | |
* | |
* @param parms | |
*/ | |
public abstract void initParms(Bundle parms); | |
/** | |
* [绑定布局] | |
* | |
* @return | |
*/ | |
public abstract int bindLayout(); | |
/** | |
* [重写: 1.是否沉浸状态栏 2.是否全屏 3.是否禁止旋转屏幕] | |
*/ | |
// public abstract void setActivityPre(); | |
/** | |
* [初始化控件] | |
* | |
* @param view | |
*/ | |
public abstract void initView(final View view); | |
/** | |
* [业务操作] | |
* | |
* @param mContext | |
*/ | |
public abstract void doBusiness(Context mContext); | |
/** View点击 **/ | |
public abstract void widgetClick(View v); | |
@Override | |
public void onClick(View v) { | |
if (fastClick()) | |
widgetClick(v); | |
} | |
/** | |
* [页面跳转] | |
* | |
* @param clz | |
*/ | |
public void startActivity(Class<?> clz) { | |
startActivity(clz, null); | |
} | |
/** | |
* [携带数据的页面跳转] | |
* | |
* @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); | |
} | |
@SuppressWarnings("unchecked") | |
public <T extends View> T $(int resId) { | |
return (T) super.findViewById(resId); | |
} | |
/** | |
* [含有Bundle通过Class打开编辑界面] | |
* | |
* @param cls | |
* @param bundle | |
* @param requestCode | |
*/ | |
public void startActivityForResult(Class<?> cls, Bundle bundle, | |
int requestCode) { | |
Intent intent = new Intent(); | |
intent.setClass(this, cls); | |
if (bundle != null) { | |
intent.putExtras(bundle); | |
} | |
startActivityForResult(intent, requestCode); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
$Log(TAG + "--->onResume()"); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
$Log(TAG + "--->onDestroy()"); | |
} | |
/** | |
* [是否允许全屏] | |
* | |
* @param allowFullScreen | |
*/ | |
public void setAllowFullScreen(boolean allowFullScreen) { | |
this.mAllowFullScreen = allowFullScreen; | |
} | |
/** | |
* [是否设置沉浸状态栏] | |
* | |
* @param allowFullScreen | |
*/ | |
public void setSteepStatusBar(boolean isSetStatusBar) { | |
this.isSetStatusBar = isSetStatusBar; | |
} | |
/** | |
* [是否允许屏幕旋转] | |
* | |
* @param isAllowScreenRoate | |
*/ | |
public void setScreenRoate(boolean isAllowScreenRoate) { | |
this.isAllowScreenRoate = isAllowScreenRoate; | |
} | |
/** | |
* [日志输出] | |
* | |
* @param msg | |
*/ | |
protected void $Log(String msg) { | |
if (isDebug) { | |
Log.d(APP_NAME, msg); | |
} | |
} | |
/** | |
* [防止快速点击] | |
* | |
* @return | |
*/ | |
private boolean fastClick() { | |
long lastClick = 0; | |
if (System.currentTimeMillis() - lastClick <= 1000) { | |
return false; | |
} | |
lastClick = System.currentTimeMillis(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment