I am using a third party library (https://github.com/heinrichreimer/material-intro) to show the introduction screens to the application, with that same library before finishing the introduction I ask for a permission to execute an AsyncTask from MainActivity,
But I do not know how to make to verify if the permission is allowed and after verifying to execute the action of the AsyncTask.
I explain how my code is, I hope you can understand me
This is the code of the Splash, start of the application:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}
In my MainActivity I have an ActivityResult to be able to show the introduction Slides for the first time and to be able to save the Sharedpreferences and not show it again. This part of my code
private class FetchNewData extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params){
HttpURLConnection urlConnection = null;
StringBuilder result = new StringBuilder();
try {
URL url = new URL( GlobalConfig.API_Content_Url);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}catch( Exception e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("sticker_packs");
cloudArray=jArray;
CompaireStickerSetsNew();
} catch (JSONException e) {
e.printStackTrace();
flashbar.dismiss();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_INTRO) {
if (resultCode == RESULT_OK) {
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putBoolean(PREF_KEY_FIRST_START, false)
.apply();
} else {
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putBoolean(PREF_KEY_FIRST_START, true)
.apply();
//User cancelled the intro so we'll finish this activity too.
finish();
}
}
}
Now I have an Activity MainIntroActivity where I use the third party library and where I show the Slides and in one specific one I ask for permission
public static final String EXTRA_PERMISSIONS = "com.heinrichreimersoftware.materialintro.demo.EXTRA_PERMISSIONS";
public static final String EXTRA_SCROLLABLE = "com.heinrichreimersoftware.materialintro.demo.EXTRA_SCROLLABLE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity mainClase = new MainActivity();
Intent intent = getIntent();
boolean permissions = intent.getBooleanExtra(EXTRA_PERMISSIONS, true);
boolean scrollable = intent.getBooleanExtra(EXTRA_SCROLLABLE, false);
setButtonBackVisible(false);
setButtonNextVisible(true);
addSlide(new SimpleSlide.Builder()
.title(R.string.title_slide_1)
.description(R.string.description_1)
.image(R.drawable.ic_launcher_background)
.background(R.color.black)
.backgroundDark(R.color.colorAccent)
.scrollable(false)
.build());
addSlide(new SimpleSlide.Builder()
.title(R.string.title_slide_2)
.description(R.string.description_2)
.image(R.drawable.ic_launcher_background)
.background(R.color.black)
.backgroundDark(R.color.colorAccent)
.scrollable(false)
.build());
final Slide permissionsSlide;
if (permissions) {
permissionsSlide = new SimpleSlide.Builder()
.title(R.string.title_permissions)
.description(R.string.description_permissions)
.background(R.color.black)
.backgroundDark(R.color.colorAccent)
.scrollable(scrollable)
.permissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE})
.build();
addSlide(permissionsSlide);
} else {
permissionsSlide = null;
}
addOnNavigationBlockedListener(new OnNavigationBlockedListener() {
@Override
public void onNavigationBlocked(int position, int direction) {
View contentView = findViewById(android.R.id.content);
if (contentView != null) {
Slide slide = getSlide(position);
if (slide == permissionsSlide) {
Snackbar.make(contentView, R.string.label_grant_permissions, Snackbar.LENGTH_LONG)
.show();
}
}
}
});
}
}
Then my problem is that when in MainActivity I don't know how to verify that the permission is already allowed by the user, then already granted the permission I should be able to execute the AsyncTask that I have in MainActivity "new FetchNewData().execute();)"
Any opinion or advice will be appreciated, greetings
CodePudding user response:
I would prefer you to use this library.
This library provides the short and simple code you want. This library provides you to handle both situations, allowed or denied.
add this implementation
to your build.gradle
file
implementation 'com.nabinbhandari.android:permissions:3.8'
add in your code
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(this/*context*/, permissions, rationale, options, new PermissionHandler() {
@Override
public void onGranted() {
// do your task.
}
@Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied, block the feature.
}
});
CodePudding user response:
You can check shouldShowRequestPermissionRationale()
in your onRequestPermissionsResult()
Check whether permission was granted or not in onRequestPermissionsResult()
If not then check shouldShowRequestPermissionRationale()
which returns either true
or false
Sample Code:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case STORAGE_PERMISSION_REQUEST:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted :)
downloadFile();
} else {
// permission was not granted
if (getActivity() == null) {
return;
}
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showStoragePermissionRationale();
} else {
Snackbar snackbar = Snackbar.make(getView(), getResources().getString(R.string.message_no_storage_permission_snackbar), Snackbar.LENGTH_LONG);
snackbar.setAction(getResources().getString(R.string.settings), new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() == null) {
return;
}
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
intent.setData(uri);
Fragment.this.startActivity(intent);
}
});
snackbar.show();
}
}
break;
}
}