Android:自定义基础类(BaseActivity)

BaseActivity:

package com.chy.yuediyun.base;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.chy.yuediyun.permission.PermissionTool;
import com.chy.yuediyun.util.ShowToast;

public abstract class BaseActivity extends AppCompatActivity {
    private static final int REQUEST_PERMISSION_CODE = 0;// 权限所用
    // 动态申请权限
    private String[] permissions = {
            Manifest.permission.INTERNET,// 网络权限
            Manifest.permission.WRITE_EXTERNAL_STORAGE,// 写入数据权限
            Manifest.permission.READ_EXTERNAL_STORAGE,// 读取数据权限
            Manifest.permission.ACCESS_FINE_LOCATION,// 定位权限
            Manifest.permission.ACCESS_COARSE_LOCATION// 获取基站的服务信号权限,以便获取位置信息
    };

    private Activity mActivity;
    private MaterialDialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setContentLayout());
        this.mActivity = this;

        getPermission();// 调用权限
        initData(savedInstanceState);


        Log.e("app",this.getClass().getSimpleName()+"-----onCreate");
    }

    /**
     * 权限
     * */
    private void getPermission(){
        if (!PermissionTool.hasPermissions(mActivity, permissions)){
            PermissionTool.requestPermissions(mActivity, REQUEST_PERMISSION_CODE, permissions);
            showToast("获取权限失败");
        }
    }

    /**
     * 子类必须实现的方法
     * */
    public abstract int setContentLayout();

    public abstract void initData(Bundle savedInstanceState);

    /**
     * 显示一个Fragment
     * */
    public void showFragment(Fragment fragment){
        if (fragment != null && fragment.isHidden()){
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.show(fragment);
            fragmentTransaction.commit();
        }
    }

    /**
     * 隐藏一个Fragment
     * */
    public void hideFragment(Fragment fragment){
        if (fragment != null && !fragment.isHidden()){
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.hide(fragment);
            fragmentTransaction.commit();
        }
    }

    /**
     * 子类直接调用展示toast
     * */
    public void showToast(String msg){
        ShowToast.showMessage(this,msg);
    }

    /**
     * 子类提供一个获取activity对象的方式
     * */
    public Activity getActivity(){
        return this;
    }


    /**
     * 一个弹窗loading库,github地址
     * implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
     * */
    public void showLoading(String loadDesc){
        mDialog = new MaterialDialog.Builder(this)
                .progress(true,-1)
                .content(loadDesc)
                .canceledOnTouchOutside(false)
                .cancelable(false)
                .show();
    }

    public void showLoading(){
        mDialog = new MaterialDialog.Builder(this)
                .progress(true,-1)
                .content("加载中...")
                .canceledOnTouchOutside(false)
                .cancelable(false)
                .show();
    }


    public void  hideLoading(){
        if (mDialog != null){
            mDialog.dismiss();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("app",this.getClass().getSimpleName()+"-----onStart");
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("app",this.getClass().getSimpleName()+"-----onRestoreInstanceState");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("app",this.getClass().getSimpleName()+"-----onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("app",this.getClass().getSimpleName()+"-----onResume");
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.e("app",this.getClass().getSimpleName()+"-----onSaveInstanceState");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("app",this.getClass().getSimpleName()+"-----onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("app",this.getClass().getSimpleName()+"-----onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("app",this.getClass().getSimpleName()+"-----onDestroy");
    }


}

实现类(MainActivity):

package com.chy.yuediyun;
import android.graphics.Color;
import android.os.Bundle;
import com.chy.yuediyun.base.BaseActivity;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;

public class MainActivity extends BaseActivity {


    // esri
    private MapView mapView;
    private GraphicsOverlay mGraphicsOverlay;// 绘制图层

    @Override
    public int setContentLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initMapView();
    }


    private void initMapView(){
        mapView = findViewById(R.id.mapView);
        // 隐藏地图下面灰白色框
        mapView.setAttributionTextVisible(false);
        mapView.setBackgroundColor(Color.RED);
        // 创建绘制图层并添加到地图中
        mGraphicsOverlay = new GraphicsOverlay();
        mapView.getGraphicsOverlays().add(mGraphicsOverlay);


    }



}

activity_main.xml:




    
    


你可能感兴趣的:(android)