1. The background

In the background of android personal privacy compliance and control, more and more strict, so in the process of app development, it is necessary to use the permission and collect user personal information to have a clear purpose and scope. Therefore, in the process of APP development, users need to use a custom dialog box to give users a clear reminder.

2. Implementation principle

The realization principle of custom dialog is mainly based on AlertDialog class. 1. Create an object for the constructor alertDialog. Builder. 2. Through the constructor object calls setTitle, setMessage, setIcon and other methods to construct the dialog box title, information, icon and other content; 3. According to the need to call setPositive/Negative/NeutralButton Negative button () method to set the front button, and the neutral button; 4. Create an AlertDialog object by calling the create method on the constructor object; 5. The AlertDialog object calls the show method to make the dialog box displayed on the interface.

3. Code implementation


 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 builder.setIcon(R.mipmap.ic_launcher);
 // Customize the title content of the dialog box
 builder.setTitle("APP will use device location information");
 // Customize the content of the dialog box
 builder.setMessage("In order to push the service more humanized, please allow the APP to access the location information. You can manage the permission through" System Settings ".);
// The disable button on the right, for the sake of simple demonstration, so the button does not trigger the event, the usual function is to change null to the corresponding trigger event function
 builder.setNegativeButton("Ban".null);
 builder.setNeutralButton("Always allow".null);
 builder.create().show();

 TelephonyManager tm = (TelephonyManager) getApplicationContext().getSystemService(Service.TELEPHONY_SERVICE);
Copy the code

4. Achieve effect display