Answer the question
In order to leave comments, you need to log in
How to give permission in android application?
Hello, I want to get a geo location, but my program is constantly crashing. And it outputs this error:
“gps” location provider requires ACCESS_FINE_LOCATION permission.
class TaskActivity : AppCompatActivity() {
var locationManager: LocationManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
...
doAsyncResult { getTask(token) }
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), 1337)
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
}
override fun onResume() {
super.onResume()
try {
locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10f, locationListener)
locationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 10, 10f, locationListener)
} catch (e: SecurityException) {
Log.d("myLogs", e.toString())
}
}
val locationListener: LocationListener = object: LocationListener {
override fun onLocationChanged(location: Location?) {
showLocation(location)
}
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderEnabled(p0: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderDisabled(p0: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
fun showLocation(location: Location?) {
if (location == null)
return
if (location.provider.equals(LocationManager.GPS_PROVIDER))
Log.d("myLogs", formatLocation(location) + " GPS")
else if (location.provider.equals(LocationManager.NETWORK_PROVIDER))
Log.d("myLogs", formatLocation(location) + " NETWORK")
}
fun formatLocation(location: Location?): String {
if (location == null)
return ""
return location.latitude.toString() + " " + location.longitude.toString() + " " + Date(location.time).toString()
}
....
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trackmove.trackmove">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.location.gps" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TaskActivity"></activity>
</application>
</manifest>
Answer the question
In order to leave comments, you need to log in
Here in this line the permissions have not yet been obtained.
You need to call this in the permission request callback.
The studio tells you otherwise. She says this method does not work on android versions < 6.0.
Regarding your question - requesting and obtaining permissions is not instantaneous. You need before
insert a check to see if permission has been granted. If received, then initialize the LocationManager, if not, then make a permissions request, and only after the user gives permission - try to initialize again.
( you can read more here )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question