Adding Permissions in AndroidManifest.xml in Android Studio? - android

In Eclipse we were able to add permissions in AndroidManifest.xml by going to AndroidManifest.xml->Permission-> Adding permissions.
How to add permissions in Android Studio? How can we get a list of all the permissions that we can add to an Activity ?

You can only type them manually, but the content assist helps you there, so it is pretty easy.
Add this line
<uses-permission android:name="android.permission."/>
and hit ctrl + space after the dot (or cmd + space on Mac). If you need an explanation for the permission, you can hit ctrl + q.

You can type them manually but the editor will assist you.
http://developer.android.com/reference/android/Manifest.permission.html
You can see the snap sot below.
As soon as you type "a" inside the quotes you get a list of permissions and also hint to move caret up and down to select the same.

Go to Android Manifest.xml
and be sure to add the <uses-permission tag > inside the manifest tag but Outside of all other tags..
<manifest xlmns:android...>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
This is an example of the permission of using Internet.

You can add manually in the manifest file within manifest tag by:
<uses-permission android:name="android.permission.CAMERA"/>
This permission is required to be able to access the camera device.

It's quite simple.
All you need to do is:
Right click above application tag and click on Generate
Click on XML tag
Click on user-permission
Enter the name of your permission.

Put these two line in your AndroidMainfest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Related

Approval for background location on Android?

I developed an Android App with Qt. Some time ago I got an E-Mail "Approval for background location" from Play Store. I'm not familiar with Java and I don't understand the problem.
My App uses GPS only if the app is visible (in foreground), but not if it is invisible (background).
My Manifest.xml looks like this:
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.BILLING"/>
Now I found on Play Store
...the ACCESS_BACKGROUND_LOCATION permission only affects an app's access to location when it runs in the background.
For me this means because I don't use ACCESS_BACKGROUND_LOCATION my App only uses GPS if it is visible (foreground). So what is the problem? Thanks...
I had a very similar problem. I double checked my AndroidManifest.xml, there was no ACCESS_BACKGROUND_LOCATION in it.
I use the QGeoPositionInfoSource from C++ to get periodic location updates. I never called the stopUpdates() function, that had to be fixed. It must be called when the app goes to background, e.g.: onPause.
Here is a very useful video with the clue, and some other useful reading material:
https://youtu.be/xTVeFJZQ28c?t=394
https://developer.android.com/training/location/permissions
https://youtu.be/xTVeFJZQ28c?t=394
https://developer.android.com/about/versions/oreo/background-location-limits
There is a signal QGuiApplication::applicationStateChanged which can be utilized for this, an example code:
if (const auto *const gui =
qobject_cast<const QGuiApplication *const>(qApp)) {
connect(gui, &QGuiApplication::applicationStateChanged, this,
[&](Qt::ApplicationState state) {
switch (state) {
case Qt::ApplicationState::ApplicationActive:
source->startUpdates();
break;
default:
source->stopUpdates();
}
});
}
The source is a pointer for the QGeoPositionInfoSource.

ACTIVITY_RECOGNITION permission is not autocompleted

I want to use the ACTIVITY_RECOGNITION
However when i type
<uses-permission android:name="com.google.android.gms.ACTI
Android Studio doesn't autocomplete this to
<uses-permission android:name="com.google.android.gms.ACTIVITY_RECOGNITION
Is that a problem? why wouldn't it autocomplete? how can i solve this issue?
thank you
Try to change your code to :
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
Following the instruction in the ActivityRecognition document and the How to Recognize User Activity With Activity Recognition tutorial.
Hope this helps.
use this permission in menifest file com.google.android.gms.permission.ACTIVITY_RECOGNITION"

Start an activity even if we lock the device

I'm developing an alarm aplication, so when the alarm goes on it start an activity with an off button
It's very simple, but my question, is there a way to start the activity even if the device is locked???
You need to use the WakeLock and also disable your keyboard for the respective Activity. They can be achieved by help of WindowManager.LayoutParams class.
WakeLock allows your app to wake up the screen i.e. turn the display on while your Activity or your complete app is running in the foreground. To learn more about implementing the WakeLock go through the following link:
http://developer.android.com/training/scheduling/wakelock.html
To disable your keyguard you need to use WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and/or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD flags for the respective Activity.
To know more about them, go to the following links:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
You will also need to use the following permissions in the Manifest file of your Android Project:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
You need the following permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Check this link

Android M permission dialog not showing

I'm currently trying to adapt my application to the new permissions model of Android M.
I'm collecting all the permissions I require, then run
Log.i("Permissions", "Requesting permissions: " + permissions);
requestPermissions(requiredPermissions.toArray(new String[requiredPermissions.size()]), requestCodeForPermissions);
requiredPermissions holds the permissions I need like android.permission.WRITE_EXTERNAL_STORAGE.
That routine is definitely executed as I have the Log line in the logcat:
08-07 12:52:46.469: I/Permissions(1674): Requesting permissions: android.permission.RECEIVE_BOOT_COMPLETED; android.permission.WRITE_EXTERNAL_STORAGE
But the permissions dialog never shows, let alone is onRequestPermissionsResult() called.
What am I doing wrong? Based on some tutorials I found I'm not missing anything.
I only have the emulator for testing, no physical device. This is the about screen from settings:
Image
It might be worth mentioning something else: If I try to open the overview of installed apps from the home screen I only get launcher3 has exited. I'm not sure if that might be related.
Does anybody have an idea why it's not showing?
I experienced the same issue but later I realized I forgot to add the permission to the manifest file. After adding the uses-permission tag, the system showed the dialog. Maybe helps someone.
The original answer helped me.
I fixed by adding tools:remove="android:maxSdkVersion" like this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
I experienced the same issue because I was using the negative value as a REQUEST_CODE.
requestPermissions(new String[]{android.Manifest.permission.CAMERA}, -1)
After using positive value, the system showed the dialog.
Hope it helps someone.
Based on the comment from Hilal (thanks a lot!):
In my case my app is indeed using tabhost and the permissions were requested from an Activity inside the tabhost. After starting a separate activity that requests the permissions it is working.
I just had the same problem.
My issue was that I wrote the permission at the wrong place in the manifest.
Make sure the uses permission is outside of application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.samples.vision.face.photo"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:hardwareAccelerated="true"
android:label="FacePhotoDemo"
android:allowBackup="true"
android:icon="#drawable/icon">
add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in AndroidManifest.xml
Note : The Permission which you want to get. Eg: android.permission.ACCESS_FINE_LOCATION etc.
I have also come across a situation where the permission dialog doesn't appear or the application crashes when using the <uses-permission-sdk23> element, however the cause appears to be a system bug on current 6.0 devices:
https://code.google.com/p/android/issues/detail?id=189841
Crash exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
I have the same issue and the problem is solved after adding the shouldShowRequestPermissionRationale like this:
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
To add to #shanraisshan's answer, the REQUEST_CODE actually has to be greater than 0, not just non-negative.
In our code, it was a simple spelling mistake.
We had:
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
It should be:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I was doing two calls to requestPermissions(), one right after the other. This error message appeared in Logcat (note the misspelling on "request"):
Can reqeust only one set of permissions at a time
requestPermissions() is actually designed to take multiple requests at once; that's what the String array is for.
In my case, the permission I was requesting (WRITE_SETTINGS) was more special and required Settings Activity to launch. So dialog was not showing up.
I had to get its permission using the following code:
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, CODE_WRITE_SETTINGS_PERMISSION);
Yet another cause for not getting the permission dialog to show when requesting a dangerous permission...
I had to do Build -> Clean Project and then Build -> Rebuild Project. I guess Android Studio didn't pick up on the changes I made in the manifest.
I had a similar issue caused by the wrong case of the permission constant in the manifest, I was using read_contacts in lower case:
<uses-permission android:name="android.permission.read_contacts" />
After changing read_contacts to uppercase it started working as expected
<uses-permission android:name="android.permission.READ_CONTACTS" />
Permissions are organised into categories so non-critical ones are granted without the dialog being shown.
I found this the hard way with internet permission, if you're having this issue then changing to a critical permission such as read_contacts will allow you to test your flow and reveal whether the issue is the permission being non-critical or something else.
Normal protection permissions are listed here
In the manifest, I changed
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
to
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
Maybe that helps.
Add permission in the manifest file as well.
If anyone of you guys has an application that changes/modifies WRITE_SETTINGS and are facing this issue. Check out CommonsWare's Answer to WRITE_SETTINGS Ambiguity
I was facing this issue for 2 weeks and later realised that this issue was due to the fact that requestPermissions doesn't work for requesting WRITE_SETTINGS permission.
Hope this helps :D
In my case I had requested "ACCESS_COARSE_LOCATION" in my manifest file and then request for "ACCESS_FINE_LOCATION" permission in code that's why the Permission Dialog was not opening.
After searching a while it appears it is required to set the value compileSdkVersion to "android-MNC" or as of today to 23. That requires the Gradle build system which then seems to require Android Studio.
At least I couldn't find a single manual about how to set it outside the gradle files.
I did all the things said in above answers but still dialog was not showing and then I changed targetSdkVersion to 23 in gradle and it appeared . Hope this helps someone
#Override
public final void validateRequestPermissionsRequestCode(int requestCode) {
// We use 16 bits of the request code to encode the fragment id when
// requesting permissions from a fragment. Hence, requestPermissions()
// should validate the code against that but we cannot override it as we
// can not then call super and also the ActivityCompat would call back to
// this override. To handle this we use dependency inversion where we are
// the validator of request codes when requesting permissions in
// ActivityCompat.
if (!mRequestedPermissionsFromFragment
&& requestCode != -1 && (requestCode & 0xffff0000) != 0) {
throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
}
}
Had the same issue. Later I realized that we have to declare each and every permission in manifest (even if one is a subclass of another).
In my case I declared
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
in my manifest and was trying to access user's coarse location.
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
Fixed the problem by adding coarse permission as well in manifest.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Set your targetSdkVersion to 22.
had the same (i guess) problem and the solution was removing
import <app package>.Manifest;
autoimported by Android Studio at the top of the file and substitute it with
import android.Manifest;
and started working
In my case the solution is the string itself
android.permission.READ_CONTACTS
I did Manifest.permission.READ_CONTACTS which caused silence error (Noting show on the screen).
Make sure that this is correct
My Android targetSDK version is 28.
I don't see any pop ups shown to user requesting for permission which are listed in android Manifest.xml as .
During both installation, installing .apk using USB and installing app from google play store.
So I added below code in my activity it will ask user permission during runtime
ActivityCompat.requestPermissions(MyActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
I was requesting permission through code, but had missed adding the <uses-permission ..> tag in manifest !
Change the final constant value to 1.
private static final int REQUEST_PERMISSION_WRITE = 1;
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_PERMISSION_WRITE);

Google maps in Android

I am trying to saw google map in emulator.
I also have obtained googleApi key for that...
but when i run my code it doesn't saw just grid insted of google map.
Any help?
Thanks......
you will have to add the below permissions in your manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
and the below library:
<uses-library android:name="com.google.android.maps" />
inside the application tag.
Do you have the necessary permissions in your manifest? You should add:
<uses-library android:name="com.google.android.maps" />
under the Application tag.
In your layout file, check if you have the correct API key:
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="example_Maps_ApiKey_String"
/>
Also, if you are just debugging, you should use the API key for the debug keystore. For instructions on how to do that, see here.
Well, I shared the same issue long time back,
I am enlisting some of the steps you MUST follow in order to get Maps Running.
You must have an AVD which is configured to target Google APIs(Google Inc) Level 'x', where x can be platform specific API level. e.g. If you are using Android 2.2 Platoform create an AVC with target as Google APIs(Google Inc) Level 8
Well when your emulator is correctly configured, you must create an android project whoes build is targeted to the specified API in step 1 or lower.
When you create your activity which would display your Map, activity class MUST extend com.google.android.maps.MapActivity (this will not be visible until you have specified correct build target) and not normal android.app.Activity. You must also implement MapAcitivities abstract method isRouteDisplayed(). You should also mention MapActivity extending class in your AndroidManifest.xml if you are created a new class(child of ),:<activity android:name="IExtendedMapAcitivity"/>
Another edit which you have to perform in your manifest is, you must mention Google API library being used, againg as you child of<application>
<uses-library android:name="com.google.android.maps" />
You should also mention required permissions in your manifest:
<uses-permission android:name="android.permission.INTERNET" />
Last and the most important step is obtaining your apiKey from google, make sure that you are using default debug keystore when obtaining that, which is present in your C:\Users\.android\debug.keystore [on windows]
using application specific keystore wont work.
Your code should look like
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="[key]"/>
</LinearLayout>
for more info on this try http://code.google.com/android/add-ons/google-apis/mapkey.html.
I hope this helps to you and all other newbies. Omiting any of the steps will result in not so correct behaviour of MapAcitivity.

Categories

Resources