in my app i need the android internet permission.
I have insert the permission in the file AndroidManifest.xml (with others permissions)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
the build.gradle defaul config have the correct api level:
defaultConfig {
applicationId "com.mytry"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
and the activity with the internet call is this:
public class ActivityLoginScreen extends Activity{
final int REQUEST_INTERNET = 1;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INTERNET)) {
//permesso giĆ richiesto in precedenza. Negato dall'utente
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, REQUEST_INTERNET);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_INTERNET: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "NO GRANTED", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
But when i launch my app and go to the activity nothing appare.
If i go in the app settings emulator, i can see only 2 permissions.
Possible solution for this problem? Where i am in wrong?
I have use this type of request in other apps without problems
Thank you.
EDIT:
For all the comments type this:
"no necessary the request because: If an app declares that it needs a normal permission, the system automatically grants the permission to the app"
this is correct.
sorry for this stupid question.
I knew the matter of default permission but my appa did not work anyway because it gave error in Internet permission.
I created a new emulator and now seems to be going.
Not all permission need to be granted by user. According to Android Documentation
System permissions are divided into two categories, normal and dangerous:
Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
Dangerous permissions can give the app access to the user's confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.
You only need to check for permission if the permission is belong to dangerous permission category (Ex: Location, Storage, Camera etc)
Internet permission are granted by default.
It does not require user to grant it, so you already have internet permission just enable your internet.
Normal permissions cover areas where your app needs to access data or
resources outside the app's sandbox, but where there's very little
risk to the user's privacy or the operation of other apps. For
example, permission to set the time zone is a normal permission. If an
app declares that it needs a normal permission, the system
automatically grants the permission to the app. For a full listing of
the current normal permissions, see Normal permissions.
As of API level 23, the following permissions are classified as PROTECTION_NORMAL:
For these no dailog will shown to user system will automatically grant the permission.
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
All dangerous Android system permissions belong to permission groups.
If the device is running Android 6.0 (API level 23)
PROTECTION_DANGEROUS permissions :
These permissions will show dialog to users.Sample Code
READ_CALENDAR
WRITE_CALENDAR
CAMERA
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
RECORD_AUDIO
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
BODY_SENSORS
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
Related
I'm trying to get the current location from either GPS or from network provider for which I'm using these permissions by declaring it in AndroidManifest.xml,
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I'm validating the permission at runtime as,
ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
and
ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
It works fine for me with Android SDK version 22 whereas in SDK version 25 though the permission is declared within AndroidManifest.xml it's not getting reflected at runtime.
I checked whether these permissions are granted or not by,
adb shell dumpsys package com.abc.xyz
The permissions were not granted for my app which runs on SDK-25 when I tried to grant the permission with adb shell as,
adb shell pm grant com.abc.xyz
android.Manifest.permission.ACCESS_COARSE_LOCATION
I'm suspecting Android made these permissions to be granted from SDK version 25 (Please correct me If I'm wrong). With just googling I'm not able to figure out how the permission level/severity changed between SDK Versions.
Please help me know about it.
On all versions of Android, your app needs to declare both the normal and the dangerous permissions it needs in its app manifest, as described in Declaring Permissions. However, the effect of that declaration is different depending on the system version and your app's target SDK level:
If your app's target SDK is 22 or lower: If you list a dangerous permission in your
manifest, the user has to grant the permission when they install the
app; if they do not grant the permission, the system does not install the app at all.
If your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited
capabilities even if the user denies a permission request.
you can refer to this Link
Try this one out.
private boolean RequestPermissions() {
int camera = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA);
int storage = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (camera != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(CAMERA);
}
if (storage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(WRITE_EXTERNAL_STORAGE);
listPermissionsNeeded.add(READ_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray
(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
this is the function that checks if the permission is needed and if they are not available it will ask from user.
Another thing you should keep in mind that ACCESS_FINE_LOCATION gives you better and accurate location and ACCESS_COARSE_LOCATION gives you less accurate location.
ACCESS_FINE_LOCATION includes ACCESS_COARSE_LOCATION. However, there is a catch:
ACCESS_COARSE_LOCATION gives you last-known location which is battery friendly https://developer.android.com/training/location/retrieve-current.html#setup
However, if you need something like live/ real-time location like Pokemon Go, use ACCESS_FINE_LOCATION
It gives you live/ real-time location.
I'm working on a Cordova app that needs the permissions INTERNET, WRITE_EXTERNAL_STORAGE, ACCESS_WIFI_STATE, and CHANGE_WIFI_MULTICAST_STATE. I have requested these permissions in the manifest like so
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
INTERNET, ACCESS_WIFI_STATE, and CHANGE_WIFI_MULTICAST_STATE are considered normal permissions and according to the docs they will be automatically accepted by Android at install time and the user can't revoke them.
Now in the MainActivity created by Cordova I added this code within an if block that checks to make sure we are on 6.0 or above:
if (checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.CHANGE_WIFI_MULTICAST_STATE) == PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "has permission");
}
else {
Log.d(TAG, "no permission");
requestPermissions(new String[] { Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
The problem is that on an Android device running 5.1.1 this works and I see all 4 permissions when I go to the permission settings of the app. However on a device running 7.0 and 7.1.1 I get a dialog asking to approve the WRITE_EXTERNAL_STORAGE and all other permissions, since they are normal, do not get asked for approval. They are automatically approved. The issue is that even though I have permission, on the newer devices I can't do things related to these permissions and if I go to the app settings for permissions all I see is permission Storage granted. Nothing for INTERNET, ACCESS_WIFI_STATE, or CHANGE_WIFI_MULTICAST_STATE. I have not tested specifically on a 6.0 device but I think I would have the same issue.
Why are the permissions not showing in the permission settings, and why cant I perform operations that need these permissions, even though Android says I have permission?
The documentation you linked to contains the following statement:
"The system doesn't prompt the user to grant normal permissions, and users cannot revoke these permissions. "
So all permissions except for WRITE_EXTERNAL_STORAGE will always be granted. The only permission you have to ask for is the one which can be revoked.
Why are the permissions not showing in the permission settings
Well, depending on the android version they may be a little hard to find. For example on my emulator running android 7.0, I found a list of all the permissions under Apps -> MyApp -> Permissions, and then clicking "All permissions" in the overflow menu.
I have a problem with bluetooth permission. I've declared in the manifest following lines:
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
What is weird that in app settings instead of bluetooth I've got a right to add permission for Location and Storage. But ok, lets say android doesn't provide specific perms for bluetooth and these are the same as BT. Weird thing is that even when I don't give any permission to the app (not for memory, not for localization), by using following code:
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_ADMIN);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "not granted :(", Toast.LENGTH_SHORT).show();
}
I still get message that permission are granted. What the heck? Should I check specifically for a EXTERNAL_STORAGE and Location permissions? And why bluetooth isn't displayed in app permision configurations?
There are two types of permissions in Android:
Dangerous permissions: These permissions are required to ask on run time if targetSdkVersion is 23 or above.
Normal Permissions: If you define these permissions in Manifest that is enough.
For more details check this link given below:
https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous
I have an Android application which need the permissions: READ_PHONE_STATE & CALL_PHONE. I've declared these permissions in the Manifest and it works fine for SDK < 23.
Now in Android 6 (SDK 23) I'm asking the user to grant this permission in runtime.
In the documentation, they say the if the user has granted one of the above permission, he actually granted all the permissions that are found in the same group permissions, in this case all the permission that are related to the phone group permissions.
But I'm facing with a problem, when I'm using telephony manager to get the deviceid, I'm getting a security exceptions that says that I don't have any access, even though the user has granted the "call phone" permission, why is that?
Update:
I'm requesting multiple permissions
In Manifest.xml
<uses-permission android:name="android.permission.CALL_PHONE">
<uses-permission android:name="android.permission.READ_PHONE_STATE">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
In Activity:
ActivityCompat.requestPermissions(this,new String[]
{
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_EXTERNAL_STORAGE
},
permissionRequestcode);
I am trying to follow the guide from google to implements permissions in my app to work with Android M. ( http://developer.android.com/training/permissions/requesting.html )
However, I am NOT able to check the <uses-permission>, my questions is: should I migrate the <uses-permission> into <permission> ?
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
My manifest permissions are this:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
To be more explicit I am able to call only this method:
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.MEDIA_CONTENT_CONTROL);
I can't check the internet, wake_lock or other <uses-permission>
I can't check the internet, wake_lock or other
That is because those are not dangerous permissions. The dangerous permissions, for Android 6.0, are:
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ADD_VOICEMAIL
BODY_SENSORS
CALL_PHONE
CAMERA
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
READ_CALENDAR
READ_CALL_LOG
READ_CELL_BROADCASTS
READ_CONTACTS
READ_EXTERNAL_STORAGE
READ_PHONE_STATE
READ_SMS
RECEIVE_MMS
RECEIVE_SMS
RECEIVE_WAP_PUSH
RECORD_AUDIO
SEND_SMS
USE_SIP
WRITE_CALENDAR
WRITE_CALL_LOG
WRITE_CONTACTS
WRITE_EXTERNAL_STORAGE
You get normal permissions, like WAKE_LOCK and INTERNET, automatically. MEDIA_CONTENT_CONTROL is a signature-level permission; ordinary Android apps cannot hold it.
I need to use <uses-permission-sdk-23> for marshmallow ?
That is for permissions that you only want to request on API Level 23+ devices, but want to skip on older devices.
should I migrate the <uses-permission> into <permission> ?
No. The framework defines the framework permissions. You would use <permission> only if you are defining some custom permission for use by third-party clients of your app's API.