1、创建类“App”
/**
* ┌───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
* │Esc│ │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│ ┌┐ ┌┐ ┌┐
* └───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘ └┘ └┘ └┘
* ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
* │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
* ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
* │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │ │
* ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
* │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter │ │ 4 │ 5 │ 6 │ │
* ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤ ┌───┐ ├───┼───┼───┼───┤
* │ Shift │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│ Shift │ │ ↑ │ │ 1 │ 2 │ 3 │ │
* ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
* │ Ctrl│ │Alt │ Space │ Alt│ │ │Ctrl│ │ ← │ ↓ │ → │ │ 0 │ . │←─┘│
* └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
*/
package com.cj.myybs.base;
import android.app.Activity;
import android.app.Application;
import java.util.Stack;
/**
* 作者: 陈骏
* 创建时间: 2018/5/22
* 博客: http://08643.cn/u/c5ada9939f6d
* Q Q: 200622550
* 作用:Application
*/
public class App extends Application {
public static final String TAG = "App";
public static App application;
private Stack<Activity> activityStack;
public static App getInstance() {
return application;
}
@Override
public void onCreate() {
super.onCreate();
application = this;
}
/***************************** activity管理 start **********************************/
/** add Activity 添加Activity到栈 */
public synchronized void addActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<Activity>();
}
activityStack.add(activity);
}
/** get current Activity 获取当前Activity(栈中最后一个压入的) */
public synchronized Activity currentActivity() {
Activity activity = activityStack.lastElement();
return activity;
}
/** 结束当前Activity(栈中最后一个压入的) */
public synchronized void finishActivity() {
Activity activity = activityStack.lastElement();
finishActivity(activity);
}
/** 结束指定的Activity */
public synchronized void finishActivity(Activity activity) {
if (activity != null) {
activityStack.remove(activity);
activity.finish();
activity = null;
}
}
public synchronized void removeActivity(Activity activity) {
if (activity != null) {
activityStack.remove(activity);
}
}
/** 结束指定类名的Activity */
public synchronized void finishActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
return;
}
}
}
/** 结束所有Activity */
public synchronized void finishAllActivity() {
for (int i = 0, size = activityStack.size(); i < size; i++) {
if (null != activityStack.get(i)) {
activityStack.get(i).finish();
}
}
activityStack.clear();
}
/** 退出应用程序 */
public void AppExit() {
try {
finishAllActivity();
} catch (Exception e) {
}
}
/***************************** activity管理 end **********************************/
}
2、创建“ActivityBase”
package com.cj.myybs.base;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
/**
* 作者: 陈骏
* 创建时间: 2018/5/22
* 博客: http://08643.cn/u/c5ada9939f6d
* Q Q: 200622550
* 作用:ActivityBase
*/
public abstract class ActivityBase extends Activity implements View.OnClickListener {
/**
* 是否沉浸状态栏
**/
private boolean isSetStatusBar = true;
/**
* 是否允许全屏
**/
private boolean mAllowFullScreen = true;
/**
* 是否禁止旋转屏幕
**/
private boolean isAllowScreenRoate = true;
/**
* 当前Activity渲染的视图View
**/
private View mContextView = null;
/**
* 日志输出标志
**/
protected final String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
if (mAllowFullScreen) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
if (isSetStatusBar) {
steepStatusBar();
}
if (!isAllowScreenRoate) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setContentView(setLayoutResourceID());
// 添加Activity到堆栈
App.getInstance().addActivity(this);
setUpView();
setUpData();
}
protected void setUpData() {
}
/***
* 用于在初始化View之前做一些事
*/
protected void init() {
}
protected abstract void setUpView();
protected abstract int setLayoutResourceID();
/**
* [沉浸状态栏]
*/
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);
}
}
/**
* [绑定控件]
*
* @param resId
* @return
*/
protected <T extends View> T $(int resId) {
return (T) super.findViewById(resId);
}
/**
* [页面跳转]
*
* @param clz
*/
public void startActivity(Class<?> clz) {
startActivity(new Intent(ActivityBase.this, clz));
}
/**
* [携带数据的页面跳转]
*
* @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);
}
/**
* [含有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 onRestart() {
super.onRestart();
Log.d(TAG, "onRestart()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
App.getInstance().removeActivity(this);
}
/**
* [简化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 allowFullScreen
*/
public void setAllowFullScreen(boolean allowFullScreen) {
this.mAllowFullScreen = allowFullScreen;
}
/**
* [是否设置沉浸状态栏]
*
* @param isSetStatusBar
*/
public void setSteepStatusBar(boolean isSetStatusBar) {
this.isSetStatusBar = isSetStatusBar;
}
/**
* 检查当前网络是否可用
*
* @param activity
* @return
*/
protected boolean isNetworkAvailable(Activity activity) {
Context context = activity.getApplicationContext();
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
3、框架添加
// 网络请求框架
compile project(':okhttp3')
//
compile project(':library')
// 注解开发
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
// Dialog对话框
compile 'com.flyco.dialog:FlycoDialog_Lib:1.2.6@aar'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.google.code.gson:gson:2.8.1'
// litepal数据库操作
compile 'org.litepal.android:core:1.6.0'
4、初始化
//1、用到litepal数据库需要在App初始化操作
LitePal.initialize(this);
//2、Okhttp初始化
private void okhttpint(){
String downloadFileDir = Environment.getExternalStorageDirectory().getPath()+"/okHttp_download/";
String cacheDir = Environment.getExternalStorageDirectory().getPath()+"/okHttp_cache";
OkHttpUtil.init(this)
.setConnectTimeout(15)//连接超时时间
.setWriteTimeout(15)//写超时时间
.setReadTimeout(15)//读超时时间
.setMaxCacheSize(10 * 1024 * 1024)//缓存空间大小
.setCacheType(CacheType.FORCE_NETWORK)//缓存类型
.setHttpLogTAG("HttpLog")//设置请求日志标识
.setIsGzip(false)//Gzip压缩,需要服务端支持
.setShowHttpLog(true)//显示请求日志
.setShowLifecycleLog(false)//显示Activity销毁日志
.setRetryOnConnectionFailure(false)//失败后不自动重连
.setCachedDir(new File(cacheDir))//设置缓存目录
.setDownloadFileDir(downloadFileDir)//文件下载保存目录
.setResponseEncoding(Encoding.UTF_8)//设置全局的服务器响应编码
.setRequestEncoding(Encoding.UTF_8)//设置全局的请求参数编码
//.setHttpsCertificate("12306.cer")//设置全局Https证书
//.addResultInterceptor(HttpInterceptor.ResultInterceptor)//请求结果拦截器
//.addExceptionInterceptor(HttpInterceptor.ExceptionInterceptor)//请求链路异常拦截器
.setCookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(this)))//持久化cookie
.build();
}
5、项目根目录拷贝文件
6、settings.gradle 添加
include ':app', ':library', ':okhttp3'
7、添加样式
<style name="loading_dialog_style" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimAmount">0.4</item>
</style>
<style name="circleProgressBarStyle">
<item name="android:indeterminateDrawable">@drawable/rotate_loading_icon</item>
<item name="android:indeterminateDuration">1500</item>
</style>