Здравствуйте, я хочу получить гео локацию, но у меня постоянно программа крашится. И выводит вот такую ошибку:
“gps” location provider requires ACCESS_FINE_LOCATION permission
.
В подсказках вот что, их пробовал, не получается. Все равно программа закрывается.
Эмулятор запускается версия sdk 27. На телефоне sdk 21. Тоже вылетает. С чем это может быть связано?
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>