Home > other >  How to make a class from that code? In order to not mess the Activity with a ton of code
How to make a class from that code? In order to not mess the Activity with a ton of code

Time:07-18

I want create a class called Permissions from below code and then call that in a click of a button. Because there is an @Override method in this activity and I don't know how to override methods inside a class. If I make a class for it the code would be much cleaner and easier to understand.

public class MainActivity extends AppCompatActivity {

TextView textView;
Button button;

final int REQUEST_CODE_FINE_LOCATION = 1234;

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

    textView = findViewById(R.id.textView);
    button = findViewById(R.id.button);

    // we are going to test weather the Location Permission is granted or not
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        textView.setText("Permission Granted...");
    } else {
        textView.setText("Permission is NOT granted");
    }
}

public void requestPermission(View view) {

    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // Permission is NOT granted
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            new AlertDialog.Builder(MainActivity.this)
                    .setMessage("We need permission for fine location")
                    .setCancelable(false)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_FINE_LOCATION);
                        }
                    })
                    .show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_FINE_LOCATION);
        }

    } else {
        // Permission is Granted
        textView.setText("Permission Granted");
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (requestCode == REQUEST_CODE_FINE_LOCATION) {

        if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Permission Granted
            textView.setText("Permission is Granted");
        } else {
            //Permission NOT granted
            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                //This block here means PERMANENTLY DENIED PERMISSION
                new AlertDialog.Builder(MainActivity.this)
                        .setMessage("You have permanently denied this permission, go to settings to enable this permission")
                        .setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                gotoApplicationSettings();
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .setCancelable(false)
                        .show();
            } else {
                //
                textView.setText("Permission NOt granted");
            }
        }
    }
}

private void gotoApplicationSettings() {

    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    Uri uri = Uri.fromParts("package", this.getPackageName(), null);
    intent.setData(uri);
    startActivity(intent);

}
}

Source: https://github.com/trulymittal/RuntimePermission

CodePudding user response:

Youc can make interface hold all the methods and create abstrct class implimenet the interface then make any classes that inhrint from the abstract class read about liskov and segrgeation dold principles

CodePudding user response:

Use TedPermission Library which is very easy to use and easy to handle . Make a Function in separate class and use it anywhere you want

    implementation 'gun0912.ted:tedpermission:2.2.3'
public void checkPermissions(Context context) {

        PermissionListener permissionlistener = new PermissionListener() {
            @Override
            public void onPermissionGranted() {
                Toast.makeText(context, "Permission Granted", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onPermissionDenied(List<String> deniedPermissions) {
                Toast.makeText(context, "Permission Denied\n"   deniedPermissions.toString(), Toast.LENGTH_SHORT).show();

            }


        };
        TedPermission.with(context)
                .setPermissionListener(permissionlistener)
                .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
                .setPermissions(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .check();
    }

https://github.com/ParkSangGwon/TedPermission

  • Related