Android系统架构-模板设计模式构建BaseActivity

 通常新建base包,然后将BaseActivity放入其中:

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局layout
        setContentView();
        //初始化头部
        initTitle();
        //初始化界面
        initView();
        //初始化数据
        initData();

    }

    protected abstract void initView();

    protected abstract void initTitle();

    protected abstract void setContentView();

    protected abstract void  initData();

    protected  T viewById(int viewId){
        return (T) findViewById(viewId);
    }
    public void showToast(String text){
        if (mToast==null){
            mToast= Toast.makeText(this,text,Toast.LENGTH_SHORT);
        }else {
            mToast.setText(text);
        }
        mToast.show();
    }

    protected boolean RequestPermission_Base( List permissions,int REQUESTCODE){
        List permissionLists = new ArrayList<>();
       for(String permission : permissions){
           if(ContextCompat.checkSelfPermission(this, permission) !=         
               PackageManager.PERMISSION_GRANTED){
               permissionLists.add(permission);
           }
        }
        if(!permissionLists.isEmpty()){//说明肯定有拒绝的权限
            ActivityCompat.requestPermissions(this, permissionLists.toArray(new 
            String[permissionLists.size()]), REQUESTCODE);
            return true;
        }else{
            return false;
        }
    }
}

在BaseActivity中封装了动态权限申请: 

public class MainActivity extends BaseActivity implements View.OnClickListener{

    Text mtv;
    @Override
    protected void initView() {
       mtv=viewById(R.id.tv);

        Button btn_normal_dialog = (Button) findViewById(R.id.btn_normal_dialog);
        Button btn_item_dialog = (Button) findViewById(R.id.btn_item_dialog);
        Button btn_single_choice = (Button) findViewById(R.id.btn_single_choice);
        Button btn_multi_choice = (Button) findViewById(R.id.btn_multi_choice);
        Button btn_custom_dialog = (Button) findViewById(R.id.btn_custom_dialog);
        btn_normal_dialog.setOnClickListener(this);
        btn_item_dialog.setOnClickListener(this);
        btn_single_choice.setOnClickListener(this);
        btn_multi_choice.setOnClickListener(this);
        btn_custom_dialog.setOnClickListener(this);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initData() {

    }
    List permissions=new ArrayList<>();
    private static final int FIND_REQUESTCODE=2;
    private void BT_FOUND_permission(){
        permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
        if (!RequestPermission_Base(permissions,FIND_REQUESTCODE)){
            mController.findDevice();
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case FIND_REQUESTCODE:
                if(grantResults.length > 0){
                    for(int grantResult : grantResults){
                        if(grantResult != PackageManager.PERMISSION_GRANTED){
                            Toast.makeText(this, "某一个权限被拒绝了", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                    mController.findDevice();
                }
                break;
            default:
                break;
        }
    }
}

你可能感兴趣的:(java)