I have an application which needs to find the user location, and location is fetched in various classes, so i have written a separate class(Not an Activity
Since public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) is an abstract method of ActivityCompat.OnRequestPermissionsResultCallback interface. See the documentation
Just implement this interface in the required class and it's done. For example
class location implements ActivityCompat.OnRequestPermissionsResultCallback{ }
Now just override onRequestPermissionsResult()
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
// case Statements
}
}
You can not override it. This method is only available for Activity and Fragments. But you can create a static method inside your Location class and call it from your activity/fragment's overridden onRequestPermissionResult method.
I have made a custom implementation for Location combined with permission. You can also use a library called Let for permissions.
1- create a transparent activity
<activity android:name=".activity.activity.CheckStoragePermissionsActivity" android:theme="@style/Theme.Transparent">
<style name="Theme.Transparent" parent="Theme.AppCompat">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
2- customize your activity
public class CheckStoragePermissionsActivity extends AppCompatActivity {
private String[] permissions;
private int pCode = 12321;
public static PermissionListener permissionListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkPermissions();
}
private void checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
boolean flag = false;
for (String s : permissions)
if (checkSelfPermission(s) != PackageManager.PERMISSION_GRANTED)
flag = true;
if (flag) {
requestPermissions(permissions, pCode);
} else {
permissionListener.permissionResult(true);
finish();
}
}else
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == pCode) {
boolean flag = true;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
for (int i = 0, len = permissions.length; i < len; i++)
if (grantResults[i] != PackageManager.PERMISSION_GRANTED)
flag = false;
if (flag) {
if (permissionListener != null)
permissionListener.permissionResult(true);
} else if (permissionListener != null)
permissionListener.permissionResult(false);
finish();
}
}
}
3- permissionListener
is an static interface and can be set directly before context.startActivity(...), or use your plan to found permission result.
public interface PermissionListener extends Serializable {
void permissionResult(boolean hasPermission);
}
4- finally call context.startActivity(new Intent(context, CheckStoragePermissionsActivity.class));
CheckStoragePermissionsActivity
do all of needed and finish after user Allow or Deny action.
You are welcome to call checkSelfPermission()
from a non-UI class, as that merely needs some Context
.
However, you must call requestPermissions()
on some activity or fragment. You override onRequestPermissionsResult()
on that same activity or fragment. This is no different than calling startActivityForResult()
and implementing onActivityResult()
.
The idea is that you request the permission before you do anything that involves your non-UI classes that are dealing with locations.
probably you can create a class that extends Activity (AppCompactActivity is better), implement all needed location code and extends with this class all activities where you need to use it.
use this class :
public class permissionModule {
final public static int STORAGE_PERMISSION_CODE = 23;
private Activity activity;
public permissionModule(Activity activity) {
this.activity = activity;
}
public void startGetPermission(){
if (isStorageReadable()){
Toast.makeText(activity,"you Already have the permission to access storage",Toast.LENGTH_SHORT).show();
}else {
requestStoragePermission();
}
}
private void requestStoragePermission() {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
private boolean isStorageReadable() {
int result = ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
}
and in your activity call :
new permissionModule(MainActivity.this).startGetPermission();
finally for result , override this in your activity :
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == permissionModule.STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar permissionWarning = Snackbar.make(rootLayout, "tnk", Snackbar.LENGTH_INDEFINITE);
permissionWarning.setAction("ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
//...
}
});
permissionWarning.show();
} else {
boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE);
if (showRationale) {
Snackbar permissionWarning = Snackbar.make(rootLayout, "Permission Required to access storage", Snackbar.LENGTH_INDEFINITE);
permissionWarning.setAction("ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
//requestStoragePermission();
}
});
permissionWarning.show();
} else {
Snackbar snackbar = Snackbar.make(rootLayout, "you denied the permission for ever pleas fix it in setting", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("setting", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
});
snackbar.show();
}
}
}
}