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 extends View> 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<String> permissions,int REQUESTCODE){
        List<String> 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<String> 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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铁铮的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值