onLocationChanged() takes too much time to call - android

In the physical device, it takes too much time to call. But in avd it's run perfectly. I test this code in redmi note 4 and zuk z2 plus. Both device location set on HIGH_ACCURACY . I want to fetch the location rapidly. But it takes too much time to execute.
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0f,wcLoactionListener)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0f,wcLoactionListener)
But if I removed this line
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0f,wcLoactionListener)
then onLocationChanged() not calling.
Here is my location listener
class MyListener:LocationListener {
override fun onLocationChanged(location: Location?) {
Log.d("Loc","*************************************************** "+ location)
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
}
Mainactivity:-
class MainActivity : AppCompatActivity() {
val LOCATION_CODE = 212
private lateinit var locationManager: LocationManager
private lateinit var cwcLoactionListener:MyListener
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cwcLoactionListener = MyListener()
checkLocationPermission()
}
#SuppressLint("MissingPermission")
fun fechLOc(){
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0f,cwcLoactionListener)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,500,0f,cwcLoactionListener)
}
private fun checkLocationPermission() {
if(Build.VERSION.SDK_INT>=23){
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED ){
requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_CODE)
return
}
}
fechLOc()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
LOCATION_CODE->{
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Accept this permission", Toast.LENGTH_LONG).show()
}else{
fechLOc()
}
}
else ->{
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
}
}
manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Related

how to create a service or process that updates location in background when application is in background?

i need a method to continuously get location updates in my application even when my application is in background. i have tried using Service and BroadcastReceiver but when application goes into background, location updates are stopped.
here is my Code with boradcast Receiver:-
MainActivity:
class MainActivity : AppCompatActivity() {
companion object {
private var instance: MainActivity? = null
fun getMainInstance(): MainActivity? {
return instance
}
}
private lateinit var client: FusedLocationProviderClient
private lateinit var request: LocationRequest
#RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
instance = this
client = LocationServices.getFusedLocationProviderClient(applicationContext)
request = LocationRequest.create()
request.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
request.interval = 1000
request.fastestInterval = 1000
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
), 2201
)
} else {
client.requestLocationUpdates(request, getPendingIntent()!!)
}
}
override fun onPause() {
super.onPause()
Toast.makeText(applicationContext, "paused", Toast.LENGTH_SHORT).show()
}
override fun onStop() {
super.onStop()
Toast.makeText(applicationContext, "stopped", Toast.LENGTH_SHORT).show()
}
override fun onDestroy() {
super.onDestroy()
client.removeLocationUpdates(getPendingIntent()!!)
Toast.makeText(applicationContext, "destroyed", Toast.LENGTH_SHORT).show()
}
fun toastMessage(location: String) {
this.runOnUiThread {
Toast.makeText(
this,
location,
Toast.LENGTH_SHORT
).show()
}
}
private fun getPendingIntent(): PendingIntent? {
val intent = Intent(this,MyLocationReceiver::class.java)
intent.setAction(MyLocationReceiver.ACTION_PROCESS_UPDATE)
return PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
}
}
BroadcastReceiver:
class MyLocationReceiver : BroadcastReceiver() {
companion object{
val ACTION_PROCESS_UPDATE = "com.example.backgroundlocation.UPDATE_LOCATION"
}
override fun onReceive(context: Context?, intent: Intent?) {
if(intent != null) {
val action = intent.action
if (action.equals(ACTION_PROCESS_UPDATE) && LocationResult.hasResult(intent)){
val locationResult = LocationResult.extractResult(intent)
try {
Log.d("onLocationReceived", locationResult.toString())
//MainActivity.getMainInstance()?.toastMessage(locationResult.toString())
} catch (e: Exception) {
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show()
}
} else {
//MainActivity.getMainInstance()?.toastMessage("no result")
}
}
}
}
to me, the solution to this seems impossible.
Is there any way to update location when application is in background ? Kindly help me out!
To access the location while in the background you need to get this permission as well from the user in the Manifest
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
While giving the permission user needs to select allow all the time for location.

onScanResult does not get called when searching for BLE device

I am trying to implement the most simple app to scan for a BLE device. I have given the necessary rights:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
However, onScanResult never gets called. Bluetooth is enabled on my phone and the BLE device is on, this is my code:
class MainActivity : AppCompatActivity() {
private val bleScanner = object :ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
Log.d("DeviceListActivity", "onScanResult()")
super.onScanResult(callbackType, result)
val device = result.device
Log.d("DeviceScanner", "Device found: ${device.address} - ${device.name ?: "Unknown"}")
//Log.d("DeviceListActivity","onScanResult: ${result.device?.address} - ${result.device?.name}")
}
}
private val bluetoothLeScanner: BluetoothLeScanner
get() {
val bluetoothManager = applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
return bluetoothAdapter.bluetoothLeScanner
}
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
Log.d("DeviceListActivity", "onCreate()")
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_device_list)
}
override fun onStart() {
Log.d("DeviceListActivity", "onStart()")
super.onStart()
enableLocation()
bluetoothLeScanner.startScan(bleScanner)
}
override fun onStop() {
Log.d("DeviceListActivity", "onStop()")
bluetoothLeScanner.stopScan(bleScanner)
super.onStop()
}
private fun enableLocation(): Boolean {
val service = applicationContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER)
return enabled
}
}
What am I missing?
Yes it was the missing requested permission. I inserted following function into onResume()
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
0
)
} else {
leDeviceScanner.start()
}
It now workes like a charm. Thanks!

Kotlin - Internet and location permissions problem

Ι am writing an android application that uses both location and internet connection permissions and I am running it on my tablet. As you can see, I have included both in the manifest file. The problem is that when OnConnected() function is called the below if in the MainActivity.kt evaluates to true and I can't proceed further in the function. So can you give me some hint to provide some more code if it is not relevant
AndroidManifest.xml :
<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" />
MainActivity.kt
override fun onConnected(p0: Bundle?) {
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
//some code
}
You have to understand the logic behind permissions in Android. Just adding the permissions in the manifest, doesn't mean that the user has authorized the application to do what those permissions enable.
If your if condition evaluates to true, since those permissions are not granted, you have to ask the user for those permissions.
Android has a some good documentation on how to this.
I highly suggest you go over that.
From Android 6.0 onwards needs to request permission the best way is to do a permissionManager Class to Handle that and then check in your Activity if the user has accepted the permissions:
class PermissionManager private constructor() {
fun checkPermission(
activity: Activity,
permissionRequests: Array<String>?,
requestCode: Int
): Boolean {
permissionRequests?.let {
it.firstOrNull {
ContextCompat.checkSelfPermission(
activity,
it
) != PackageManager.PERMISSION_GRANTED
}?.apply {
ActivityCompat.requestPermissions(
activity,
permissionRequests,
requestCode
)
return false
}
}
return true
}
companion object {
val instance by lazy { PermissionManager() }
const val CAMERA_PERMISSION = 1
}
}
Then in your activity check the permission Result:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ar)
//If the user has the permission accepted the next times
if (PermissionManager.instance.checkPermission(
this, arrayOf(
Manifest.permission.CAMERA
), PermissionManager.CAMERA_PERMISSION
)
) {
initLogic()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
PermissionManager.CAMERA_PERMISSION -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initLogic()
} else {
return
}
}
}
}
}

Get current location Android Kotlin

I try to get the current Location with GM API in my application (using Android Studio). But if i click the button which triggers the getLocation() funktion, i always end up in the catch{} block and i dont know why.
My mobile device is connected for testing.
Here is the getLocation() Funktion:
fun getLocation() {
var locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?
var locationListener = object : LocationListener{
override fun onLocationChanged(location: Location?) {
var latitute = location!!.latitude
var longitute = location!!.longitude
Log.i("test", "Latitute: $latitute ; Longitute: $longitute")
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
}
try {
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
} catch (ex:SecurityException) {
Toast.makeText(applicationContext, "Fehler bei der Erfassung!", Toast.LENGTH_SHORT).show()
}
}
Here is the onCreate Funktion:
class CurrentLocationActivity : AppCompatActivity() {
lateinit var mapFragment : SupportMapFragment
lateinit var googleMap : GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_current_location)
//Karte erstellen
mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(OnMapReadyCallback {
googleMap = it
})
//OnClickListener um Location zu speichern
btnGetCurrentLocation.setOnClickListener {
getLocation()
}
}
Try as follow
Step 1. Put on your AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com....">
<!-- This line -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application ... />
</manifest>
Step 2. Put it above your location request
import android.Manifest
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
...
fun getLocation() {
...
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSION_REQUEST_ACCESS_FINE_LOCATION)
return
}
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_ACCESS_FINE_LOCATION) {
when (grantResults[0]) {
PackageManager.PERMISSION_GRANTED -> getLocation()
PackageManager.PERMISSION_DENIED -> //Tell to user the need of grant permission
}
}
}
companion object {
private const val PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 100
}
The LocationManager will throw a SecurityException if the location permission has not been granted.
Information on adding the location permissions to your app can be found here.

List BLE devices Android - Kotlin

I'm trying to list all BLE devices on an Android device, using Kotlin (the Java-version don't work either) but I don't get any devices or any call back at all, except for a "scan was already started"
I have the correct uses-permission in the manifest.
Here is the current minimum of code, I'm trying with.
But even the sample code from Google is listing any devices.
I'm running on a Pixel with Android version 8.1.0.
I have it working on iOS, with the basic BLE device list (Swift)!
private val bleScanner = object :ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
super.onScanResult(callbackType, result)
Log.d("DeviceListActivity","onScanResult: ${result?.device?.address} - ${result?.device?.name}")
}
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
super.onBatchScanResults(results)
Log.d("DeviceListActivity","onBatchScanResults:${results.toString()}")
}
override fun onScanFailed(errorCode: Int) {
super.onScanFailed(errorCode)
Log.d("DeviceListActivity", "onScanFailed: $errorCode")
}
}
private val bluetoothLeScanner: BluetoothLeScanner
get() {
val bluetoothManager = applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter
return bluetoothAdapter.bluetoothLeScanner
}
class ListDevicesAdapter(context: Context?, resource: Int) : ArrayAdapter<String>(context, resource)
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("DeviceListActivity", "onCreate()")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_device_list)
}
override fun onStart() {
Log.d("DeviceListActivity","onStart()")
super.onStart()
bluetoothLeScanner.startScan(bleScanner)
}
override fun onStop() {
bluetoothLeScanner.stopScan(bleScanner)
super.onStop()
}
You need to declare permission:
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
and you for devices with android 6.0 + request manually this permission:
override fun onStart() {
Log.d("ScanDeviceActivity", "onStart()")
super.onStart()
when (PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
PackageManager.PERMISSION_GRANTED -> bluetoothLeScanner.startScan(bleScanner)
else -> requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), 1)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
1 -> when (grantResults) {
intArrayOf(PackageManager.PERMISSION_GRANTED) -> {
Log.d("ScanDevices", "onRequestPermissionsResult(PERMISSION_GRANTED)")
bluetoothLeScanner.startScan(bleScanner)
}
else -> {
Log.d("ScanDevices", "onRequestPermissionsResult(not PERMISSION_GRANTED)")
}
}
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}

Categories

Resources