package com.shlb.comb.base;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.view.View;
|
import android.view.inputmethod.InputMethodManager;
|
import android.widget.ImageView;
|
import android.widget.TextView;
|
|
import androidx.annotation.Nullable;
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import com.blankj.utilcode.util.SnackbarUtils;
|
import com.blankj.utilcode.util.ToastUtils;
|
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
|
import com.shlb.comb.R;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import java.util.List;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
import pub.devrel.easypermissions.EasyPermissions;
|
|
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {
|
public String TAG = this.getClass().getSimpleName();
|
//title左边按钮
|
protected ImageView iv_left;
|
//title标题
|
protected TextView tv_center;
|
//title右边按钮
|
protected ImageView iv_right;
|
|
|
@Override
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
contentView();
|
EventBus.getDefault().register(this);
|
init();
|
}
|
|
private void init() {
|
initView();
|
initData();
|
initEvent();
|
}
|
|
protected void initHead() {
|
iv_left = findViewById(R.id.iv_left);
|
tv_center = findViewById(R.id.tv_center);
|
iv_right = findViewById(R.id.iv_right);
|
initHeadEvent();
|
}
|
|
|
private void initHeadEvent() {
|
iv_left.setOnClickListener(this);
|
iv_right.setOnClickListener(this);
|
}
|
|
protected abstract void contentView();
|
|
protected abstract void initView();
|
|
protected abstract void initData();
|
|
protected abstract void initEvent();
|
|
|
@Override
|
public void setTitle(CharSequence title) {
|
tv_center.setText(title);
|
}
|
|
@Override
|
public void onClick(View v) {
|
int id = v.getId();
|
if (id == R.id.iv_left) {
|
finish();
|
} else if (id == R.id.iv_right) {
|
}
|
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
EventBus.getDefault().unregister(this);
|
}
|
|
QMUITipDialog tipDialog;
|
|
//弹出提示
|
public void showTips(String tips) {
|
tipDialog = new QMUITipDialog.CustomBuilder(this)
|
.setContent(R.layout.tipdialog_custom)
|
.create();
|
tipDialog.setCanceledOnTouchOutside(true);
|
((TextView) tipDialog.findViewById(R.id.tv_tip)).setText(tips);
|
tipDialog.show();
|
|
new Timer().schedule(new TimerTask() {
|
@Override
|
public void run() {
|
if (tipDialog != null)tipDialog.dismiss();
|
}
|
}, 1500);
|
// LogUtils.e("TAG","准备");
|
// handler.postDelayed(new Runnable() {
|
// @Override
|
// public void run() {
|
// LogUtils.e("TAG","进入");
|
// if (tipDialog != null) {
|
// LogUtils.e("TAG","关闭");
|
// tipDialog.dismiss();
|
// }
|
// }
|
// }, 1500);
|
}
|
QMUITipDialog loading;
|
public void showLoading(){
|
showLoading("正在加载");
|
}
|
|
public void showLoading(String msg){
|
if(loading != null && loading.isShowing()){
|
loading.dismiss();
|
}
|
loading = new QMUITipDialog.Builder(this)
|
.setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
|
.setTipWord(msg)
|
.create();
|
loading.show();
|
}
|
|
public void hiddeLoading(){
|
if (loading != null) {
|
loading.dismiss();
|
}
|
}
|
|
|
/**
|
* 强制隐藏键盘
|
*/
|
protected void hideKeyborad() {
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
imm.hideSoftInputFromWindow(iv_left.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
|
}
|
|
/**
|
* 打开至指定的Activity
|
*
|
* @param pClass
|
*/
|
public void openActivity(Class<?> pClass) {
|
openActivity(pClass, null);
|
}
|
|
/**
|
* 打开至指定的Activity
|
*
|
* @param pClass
|
* @param pBundle 传值
|
*/
|
public void openActivity(Class<?> pClass, Bundle pBundle) {
|
Intent intent = new Intent(this, pClass);
|
if (pBundle != null) {
|
intent.putExtras(pBundle);
|
}
|
startActivity(intent);
|
}
|
|
/**
|
* 打开activity
|
*
|
* @param pAction activity动作
|
*/
|
public void openActivity(String pAction) {
|
openActivity(pAction, null);
|
}
|
|
/**
|
* 打开activity
|
*
|
* @param pAction activity动作
|
* @param pBundle 数据
|
*/
|
public void openActivity(String pAction, Bundle pBundle) {
|
Intent intent = new Intent(pAction);
|
if (pBundle != null) {
|
intent.putExtras(pBundle);
|
}
|
startActivity(intent);
|
}
|
|
/**
|
* 返回activity ,绑定数据
|
*
|
* @param pClass
|
*/
|
public void returnActivity(Class<?> pClass) {
|
returnActivity(pClass, null);
|
}
|
|
/**
|
* 返回activity ,绑定数据
|
*
|
* @param pClass
|
* @param pBundle
|
*/
|
public void returnActivity(Class<?> pClass, Bundle pBundle) {
|
Intent intent = new Intent(this, pClass);
|
if (pBundle != null) {
|
intent.putExtras(pBundle);
|
}
|
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
startActivity(intent);
|
}
|
|
/**
|
* 返回activity
|
*
|
* @param pAction activity动作
|
*/
|
public void returnActivity(String pAction) {
|
returnActivity(pAction, null);
|
}
|
|
/**
|
* 返回activity
|
*
|
* @param pAction activity动作
|
* @param pBundle 数据
|
*/
|
public void returnActivity(String pAction, Bundle pBundle) {
|
Intent intent = new Intent(pAction);
|
if (pBundle != null) {
|
intent.putExtras(pBundle);
|
}
|
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
startActivity(intent);
|
}
|
|
public void Toast(String text) {
|
ToastUtils.showShort(text);
|
// SnackbarUtils.with(tv_center).setDuration(SnackbarUtils.LENGTH_LONG).setMessage(text).show();
|
}
|
@Override
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
// Forward results to EasyPermissions
|
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
|
}
|
|
|
|
@Override
|
public void onPermissionsGranted(int requestCode, List<String> list) {
|
// Some permissions have been granted
|
Toast("权限检测通过!");
|
}
|
|
@Override
|
public void onPermissionsDenied(int requestCode, List<String> list) {
|
// Some permissions have been denied
|
Toast("未打开所需权限,程序无法正常运行"+list.toString());
|
}
|
|
}
|