为什么我的Activity没有在onResume中调用

时间:2017-01-22 00:44:09

标签: java android

昨天运行的应用程序今天不起作用。我还是不明白自己做错了什么。问题是,我正在调用以下代码片段,但我无法激活它。有什么问题?

EDIT1: 吐司有效但为什么它中的粒子不起作用?

public class PermissionsActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);

    }

    @Override
    protected void onStart() {
        super.onStart();
        initWidget();
    }


    private void initWidget() {

        Button permission = (Button) findViewById(R.id.permission);
        permission.setOnClickListener(this);

        Button accessibility = (Button) findViewById(R.id.accessibility);
        accessibility.setOnClickListener(this);


        LinearLayout permissionLayout = (LinearLayout) findViewById(R.id.permission_layout);


        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            permissionLayout.setVisibility(View.GONE);
        } else {
            permissionLayout.setVisibility(View.VISIBLE);
        }
    }

    private void goToPassword() {
        if (SharedPreferenceUtil.readIsNumModel()) {
            startActivity(new Intent(this, NumberCheckActivity.class));
            finish();
        } else {
            startActivity(new Intent(this, GestureCheckActivity.class));
            finish();
        }
        SharedPreferenceUtil.editIsFirst(false);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 2909: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    ToastUtils.showToast("Permission allowed.");

                } else {
                    ToastUtils.showToast("Please allow permission");
                }
                return;
            }
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.permission:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.System.canWrite(this)) {
                        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2909);

                    } else {

                    }
                } else {
                    ToastUtils.showToast("Success");
                }

                break;

            case R.id.accessibility:
                if (!MyConstants.isPermissionOk(PermissionsActivity.this)) {
                    PermissionsActivity.this.startActivity(new Intent("android.settings.USAGE_ACCESS_SETTINGS"));
                    ToastUtils.showToast("Please find App Locker in this menu and enable it.");
                } else {
                    ToastUtils.showToast("Accessibility enabled");
                }
                break;


            default:
                break;
        }
    }


    protected void onResume() {
        super.onResume();
        goToNextScreen();

    }

    private void goToNextScreen() {

        if (Build.VERSION.SDK_INT >= 23) {
            if (MyConstants.isPermissionOk(PermissionsActivity.this) && Settings.System.canWrite(this)) {
                goToPassword();
            }
        }
        ToastUtils.showToast("onResume Working?");
    }
}

问题已修复 - 请查看最佳解决方案:

public class PermissionsActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);

    }

    @Override
    protected void onStart() {
        super.onStart();
        initWidget();
    }


    private void initWidget() {

        Button permission = (Button) findViewById(R.id.permission);
        permission.setOnClickListener(this);

        Button accessibility = (Button) findViewById(R.id.accessibility);
        accessibility.setOnClickListener(this);


        LinearLayout permissionLayout = (LinearLayout) findViewById(R.id.permission_layout);


        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            permissionLayout.setVisibility(View.GONE);
        } else {
            permissionLayout.setVisibility(View.VISIBLE);
        }
    }

    private void goToPassword() {
        if (SharedPreferenceUtil.readIsNumModel()) {
            startActivity(new Intent(this, NumberCheckActivity.class));
            finish();
        } else {
            startActivity(new Intent(this, GestureCheckActivity.class));
            finish();
        }
        SharedPreferenceUtil.editIsFirst(false);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 2909: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    ToastUtils.showToast("Permission allowed.");

                } else {
                    ToastUtils.showToast("Please allow permission");
                }
                return;
            }
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.permission:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.System.canWrite(this)) {
                        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2909);

                    } else {
                        ToastUtils.showToast("Success");
                    }
                } else {
                    ToastUtils.showToast("Success");
                }

                break;

            case R.id.accessibility:
                if (!MyConstants.isPermissionOk(PermissionsActivity.this)) {
                    PermissionsActivity.this.startActivity(new Intent("android.settings.USAGE_ACCESS_SETTINGS"));
                    ToastUtils.showToast("Please find App Locker in this menu and enable it.");
                } else {
                    ToastUtils.showToast("Accessibility enabled");
                }
                break;


            default:
                break;
        }
    }


    protected void onResume() {
        super.onResume();
        goToNextScreen();

    }

    private boolean checkIfAlreadyhavePermission() {
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void goToNextScreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (MyConstants.isPermissionOk(PermissionsActivity.this) && checkIfAlreadyhavePermission() ) {
                goToPassword();
            }
        }else{
            goToPassword();
        }
    }
}

0 个答案:

没有答案