Checking permission - android

My app uses Internet access to display ads so I have the Internet permission declared in Manifest.
With latest Android releases, the user can disable this permission.
My problem is that if the user cancels this permission, the app is not able to load ads.
I know I can check if there is any current connection, in that case I will not try to load the app. But I want to know if the user has denied that permission in Android settings.
The idea is to prompt the user to enable it or the app will not run.
I add a screenshot of the Huawei Android Pie where the user can modify these settings

You just need to check first that whether the user enable (Allow the permission) or not. If not then redirect the user to the phone setting so that the user allow that permission otherwise just finish the app.
if (ContextCompat.checkSelfPermission(this,Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED )
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// if user blocked permission
android.app.AlertDialog.Builder dlgAlert = new android.app.AlertDialog.Builder(this);
dlgAlert.setPositiveButton("Take me to app settings",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
});
dlgAlert.setNegativeButton("Cancel",null);
dlgAlert.setCancelable(true);
dlgAlert.setIcon(R.drawable.appicon);
dlgAlert.setMessage("The app needs the permission please allow it other wise you cannot proceed.");
dlgAlert.setTitle("Oops!");
dlgAlert.create().show();
}
}
else
{
finish();
}

If internet connection is all you want to check, you dont have to check permissions for this because Internet is not a critical permission. What you can simply do is use ConnectivityManager class to check if the device has active internet connection or not and then write your logic accordingly.
Refer: This on how you can check for internet connectivity.

Related

Background service getting killed on removing the app from task manager on ColorOS

I am working on an app where one of the service needs to be running always to perform some specific operation in the background. So i am restarting the service using a Broadcast Receiver, whenever it is being killed from the task manager. So for this i am taking Autostart/Battery Optimization permission from the user to get it restarted again.
This approach is working completely fine with almost every leading device manufacturers except on ColorOS and as long as the "Autostart/Battery Optimization permission" is turned on for my app, it is working completely fine on every other devices except that on ColorOS.
The reason for this being, i am not able to redirect user to the "AutoStart" or "Battery Optimization" settings page
I have tried to open the Autostart settings Activity from my app using this code:
Intent autostartIntent = new Intent();
autostartIntent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
startActivity(autostartIntent);
Also i have tried to play with the power saving settings manually to check if in any case that is working. But nothing seems to be working anyways.
I would be looking a way to redirect user to the Autostart permission page or to the battery optimization settings page. Anyone who dealt with the similar kind of problem can suggest some solution or even workarounds for the same.
Got it working!!
I have redirected user to the app details page and there he/she need to turn on Auto Startup option. This will keep the service running on ColorOS. Below is the code to redirect user to the app details page and here the user need to turn the Auto Startup on.
if ("oppo".equalsIgnoreCase(Build.MANUFACTURER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);
builder.setTitle("Allow Auto-startup");
builder.setMessage("To access content on lock screen, enable ‘Allow Auto-startup’ for JiffyFeed");
builder.setPositiveButton("Allow",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
});
builder.show();
}
Additionally, i have used a workaround as well. I am using NotificationListenerService which keeps on running always, so i am restarting the service on receipt of a new notification, so every time a new notification comes up, it wakes up the service which i need to keep running always.

Android on demand permission request

How to handle don't ask me again in android on demand permission request. Is their any best practice if the user chooses don't ask me again and later if he want to get access any best practice suggestion? Thanks.
shouldShowRequestPermissionRationale will help you with this.
shouldShowRequestPermissionRationale() will be used to check whether user have selected Never ask again. It will return value as True or False, If user have selected Never ask before it will return false, else will return true.
boolean shouldShow = shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE);
if (!shouldShow) {
// show some dialog which will give information about required permission, so that user can understand what is need of that permission.
}
on button click of dialog, you can redirect user to setting screen of app.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
If you are working for pre-marshmallow, you need to use ActivityCompat.shouldShowRequestPermissionRationale()

Provide custom text for Android M permission dialog

Is it possible to provide a custom text for the system dialog which is displayed when the user is asked to grant permission?
No, you can't customize the text of the dialog, but you can provide an explanation before request the permission. Quoting from developer.android.com:
Request Permissions
If your app needs a dangerous permission that was listed in the app
manifest, it must ask the user to grant the permission. Android
provides several methods you can use to request a permission. Calling
these methods brings up a standard Android dialog, which you cannot
customize.
Explain why the app needs permissions
In some circumstances, you might want to help the user understand why
your app needs a permission. For example, if a user launches a
photography app, the user probably won't be surprised that the app
asks for permission to use the camera, but the user might not
understand why the app wants access to the user's location or
contacts. Before you request a permission, you should consider
providing an explanation to the user. Keep in mind that you don't want
to overwhelm the user with explanations; if you provide too many
explanations, the user might find the app frustrating and remove it.
One approach you might use is to provide an explanation only if the
user has already turned down that permission request. If a user keeps
trying to use functionality that requires a permission, but keeps
turning down the permission request, that probably shows that the user
doesn't understand why the app needs the permission to provide that
functionality. In a situation like that, it's probably a good idea to
show an explanation.
To help find situations where the user might need an explanation,
Android provides a utiltity method,
shouldShowRequestPermissionRationale(). This method returns true if
the app has requested this permission previously and the user denied
the request.
We cannot customize request permission dialog but we can provide user a custom explanation that why we are requesting below is the method with custom explanation
private void checkForCameraPermission() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Camera permission necessary");
alertBuilder.setMessage("FITsociety need camera permission to read barcode.");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BarCodeScannerActivity.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
// MY_PERMISSIONS_REQUEST_CAMERA is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
setBarCodeScannerView();
}
}
the above method check whether permission is already granted if not then it check if custom explanation is required with this method
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)
the documentation for this method is here shouldShowRequestPermissionRationale() this method return true only if user deny to permission dialog or user close the permission from the setting of the application if user did so then show alert dialog with custom explanation and proceed further hope it works

How can i detect the change in network state and reload my android app if the connectivity is available

I have an android app which runs when internet connection is active. When the connection is not available, a dialog box appears through which wifi can be activated. I have made the app to quit when connection is not available after initiating wifi connection. The question is, i have to reload the app and continue using the app when the internet connection is available after enabling the wifi rather than to exit the app.
Here is the code:
if (CheckConnection.checkInternetConnection(this)) {
//load a webview and show contents from web.
}
else
{
try {
AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet Connection not available. Please cross check your internet connectivity and try again.");
alertDialog.setIcon(R.drawable.error);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);
alertDialog.setButton2("WiFi Setting", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
startActivity(intent);
System.exit(0);
}
});
alertDialog.show();
}
catch(Exception e)
{
}
Don't call System.exit(0).
First of all because it is not a recommended way of quitting your app and makes it look to the user as if it crashed.
Second, why are you exiting at all? The user will be navigated to the internet settings, switch them on and when they press back, your activity will already be there. You can recheck internet status onResume of your activity. This is the acceptable way of handling an unavailable internet connection in your application.

Ask user to turn on Wi-Fi

I have an app which almost always needs to know the user location.
When I need to access a location, I do this:
final AlertDialog.Builder builder = new AlertDialog.Builder(
MapScreen.this);
builder.setTitle("MyAppName");
builder.setMessage("The location service is off. Do you want to turn it on?");
builder.setPositiveButton("Enable location",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
final DialogInterface dialogInterface,
final int i) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("Continue without location", null);
builder.create().show();
However, the GPS gives me some info that isn't always precise enough. Wi-Fi always gives me enough precission, so I want to ask the user to turn on the Wi-Fi the same way I ask them to turn on the location. I do not want to just turn it on, I want the user to be notified about it, and to manually enable it.
Is there any Intent to bring the WiFi menu to the user?
Following intent shows the wireless settings such as Wi-Fi, Bluetooth and Mobile networks:
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
For the complete list of Settings: https://developer.android.com/reference/android/provider/Settings.html
For the documentation on the startActivity method:
https://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)
(keep in mind startActivity is just throw and forget, if you want to capture the response of what the user did out there you could instead call startActivityForResult, probably not needed in this case)
You can try
Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(i);
The previous answers were quotting the "Settings.ACTION_WIRELESS_SETTINGS", but it is too general, as it is going to open the settings of the general wireless connection while the question is about wifi only.
I'm using "Settings.ACTION_WIFI_SETTINGS" as follows
Intent turnWifiOn = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(turnWifiOn);
Note: It's not going to open just a dialog, but an activity instead.

Categories

Resources