Android的一个BaseActivity

723 阅读1分钟
 

/**
 * 省去引用,create by Partin 2019/8/7
 */

public class BaseActivity extends PermissionsBaseActivity {

    public static BaseActivity frontActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Activity的集合
        ActivityCollector.addActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        //系统状态栏上色
        SystemBarTintManager.setTitleActivity((ViewGroup) findViewById(R.id.fl_statusbar), this, R.color.title_bg);
    }
    /*
    *无参数跳转activity的话直接使用gotoActivity
    */
    public void gotoActivity(Class<?> activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    /**
     * 请求权限
     *
     * @param id                   请求授权的id 唯一标识即可
     * @param permission           请求的权限
     * @param allowableRunnable    同意授权后的操作
     * @param disallowableRunnable 禁止权限后的操作
     */
    @Override
    protected void requestPermission(int id, String permission, Runnable allowableRunnable, Runnable disallowableRunnable) {
        if (allowableRunnable == null) {
            throw new IllegalArgumentException("allowableRunnable == null");
        }

        allowablePermissionRunnables.put(id, allowableRunnable);
        if (disallowableRunnable != null) {
            disallowablePermissionRunnables.put(id, disallowableRunnable);
        }

        //版本判断
        if (Build.VERSION.SDK_INT >= 23) {
            //减少是否拥有权限
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                //弹出对话框接收权限
                ActivityCompat.requestPermissions(BaseActivity.this, new String[]{permission}, id);
                return;
            } else {
                allowableRunnable.run();
            }
        } else {
            allowableRunnable.run();
        }
    }

   /**
   *返回上一级,一般用于header上的返回键,写入xml
   */
    public void TitleBackClick(View view) {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
        View focusedView = getCurrentFocus();
        /*
         * If no view is focused, an NPE will be thrown
         *
         * Maxim Dmitriev
         */
        if (focusedView != null) {
            inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
        onBackPressed();
    }

    static Toast toast;
    /*
    *Toast方法
    */
    public static void showToast(final String string) {
        if (TextUtils.isEmpty(string))
            return;
        Log.e("toast", string);
        if (frontActivity != null)
            frontActivity.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {

                                                if (toast != null) {
                                                    toast.cancel();
                                                    toast = Toast.makeText(frontActivity, string, Toast.LENGTH_SHORT);

                                                } else {
                                                    toast = Toast.makeText(frontActivity, string, Toast.LENGTH_SHORT);
                                                }
                                                toast.show();
                                            }
                                        }
            );
    }




    public void showToast(int id) {
        showToast(getResources().getString(id));
    }

    public static void initChidrenViewLisener(View view, View.OnClickListener clickListener) {
        if (view instanceof TextView) {
            view.setOnClickListener(clickListener);
        } else {
            if (view instanceof ViewGroup) {
                for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                    initChidrenViewLisener(((ViewGroup) view).getChildAt(i), clickListener);
                }
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    //在onResume()方法注册
    @Override
    protected void onResume() {
        frontActivity = this;

        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
        ActivityCollector.removeActivity(this);
    }

}