Android closes permissions in application settings and returns to lifecycle processing

Keywords: Mobile Android Permission denied

problem

In dealing with the 6.0 runtime permissions, many people have overlooked such a problem:
In an App application, if you have allowed a privilege such as (read communication privilege), call the camera at this moment, pop up the privilege application dialog box, click reject at this moment, and then pop up after processing to set the privilege interface (because the privilege is always to apply). If you open the application privilege settings in the place interface, you will not open the camera privilege at this moment, but will have to. What happens when the access to the address book is closed? The flow chart is as follows:

  • Original graph

01.png

  • Step 1: Open the communication permission and click Allow

02.png

  • Step 2: Open the camera application permission and click Deny

03.png

  • Step 3: Set up the dialog box after the camera permission is denied

To set. png

  • Step 4: Click Settings to Open Application Settings

     

    04.png

  • Step 5: Click on permission, open permission settings, and close the permission to read the address book that has been successfully applied for.

05.png

06.png

  • Step 6, click the return button and go back to your App

Analysis

After the above steps, we can find that when the permission is closed, the application will restart, and the user interface for permission application will be opened directly by default, and the startup page will not be opened. Why? Because when the permission is closed, the program will be murdered, the Activity will end abnormally, and when returned, the app will default to fix the last open interface.

Confirm

The analysis above confirms one by one that in order to avoid the privilege application interface being the startup page interface, I will write two interfaces, the interface jump step is

LanchActivity---->MainActivity---->PremissionActivity

  • LanchActivity

LanchActivity.png

1501507953(1).png

  • MainActivity

MainActivity.png

1501507990(1).png

  • PremissionActivity

     

    PremissionActivity.png

1501508027(1).png

  • MyApplication for Application Restart Judgment

1501507895(1).png

Startup program

Start the program and follow the steps above to close the permission and return it to see the print results.

image.png

 

You can see clearly that the Application is executed again, and the PremissionActivity interface will be reloaded, and saved Instance State is not empty! You can also know that the program will not start the start page LanchActivity at this time!

What happens when you press the return key? Look at the print results:

image.png

 

You can see that you will return to the startup page as normal, but both the startup page and the home interface are abnormally killed, so saved Instance State will have data.

summary

With the above tests, we can know the life cycle of the program after the permission is forcibly closed. Sometimes the program has a lot of data. When the permission is closed back, some data can not be recovered and the null pointer abnormality will occur. So here we can follow the design idea of Wechat:

After forcibly closing permissions, let the program reopen the startup page!

The treatment is as follows:

image.png

Difficult problems, shut down the program and return to the white screen will appear a problem! uuuuuuuuuu At present, we do not know how to solve this problem, please inform us!

PremissionActivity code:

public class PremissionActivity extends AppCompatActivity {
    private static final int REQUEST_CAMERA_PERMISSION = 0x103;
    private static final int REQUEST_PRE_SET = 0x104;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("PNH", "PremissionActivity start-up savedInstanceState=" + savedInstanceState);


        if (null != savedInstanceState) {
            Intent intent = new Intent(this, LanchActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
        setContentView(R.layout.activity_premission);
        findViewById(R.id.camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(PremissionActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(PremissionActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                }
            }
        });
        findViewById(R.id.contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Check whether permissions have been granted
                if (ContextCompat.checkSelfPermission(PremissionActivity.this, Manifest.permission.READ_CONTACTS)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(PremissionActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 3);
                }
            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                //Permission denied
                openAppSetting();
            }
        }
    }

    protected void openAppSetting() {
        MaterialDialog materialDialog = new MaterialDialog.Builder(this)
                .content("Set up-application-GcsSloop-Open the camera permission in the permission to normal use of photography, small video, sweep and other functions")
                .positiveText("To set up")
                .negativeText("cancel")
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    }
                })
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

//                        Intent intent = new Intent("/");
//                        ComponentName cm = new ComponentName("com.android.settings","com.android.settings.ManageApplications");
//                        intent.setComponent(cm);
//                        intent.setAction("android.intent.action.VIEW");
//                        startActivityForResult( intent , 0);

//                        Open Privilege Setting Interface
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + getPackageName()));
//                        Application permission returns to execution
                        startActivityForResult(intent, REQUEST_PRE_SET);
                    }
                }).build();
        materialDialog.show();


    }

}

 

Posted by cybersurfur on Mon, 21 Jan 2019 09:27:13 -0800