Skip to content

Commit

Permalink
Android 34 support fix for registerReceiver issue
Browse files Browse the repository at this point in the history
  • Loading branch information
naveenr-egov committed Dec 19, 2024
1 parent b6ff46b commit d12b9e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/flutter-build-apk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
build-apk:
runs-on: ubuntu-latest
environment: QA
environment: UAT

steps:
- name: Checkout code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (flutterVersionName == null) {


android {
compileSdkVersion 34
compileSdk = flutter.compileSdkVersion
ndkVersion flutter.ndkVersion

compileOptions {
Expand All @@ -46,8 +46,8 @@ android {
applicationId "com.digit.hcm"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.digit.hcm

import io.flutter.embedding.android.FlutterActivity
import android.os.Bundle
import java.io.File
import java.io.FileOutputStream
import io.flutter.plugin.common.MethodChannel
import android.content.Intent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Environment
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
private val CHANNEL = "com.digit.location_tracker"
Expand All @@ -23,44 +19,64 @@ class MainActivity : FlutterActivity() {
val accuracy = intent?.getFloatExtra("accuracy", 0.0f) // Retrieve accuracy here

// Handle the location data here
Toast.makeText(context, "Latitude: $latitude, Longitude: $longitude, Accuracy: $accuracy", Toast.LENGTH_LONG).show()
Toast.makeText(
context,
"Latitude: $latitude, Longitude: $longitude, Accuracy: $accuracy",
Toast.LENGTH_LONG
).show()
// Optionally, you can send this data to Flutter via MethodChannel
flutterEngine?.dartExecutor?.binaryMessenger?.let {
MethodChannel(it, CHANNEL).invokeMethod("locationUpdate", mapOf(
"latitude" to latitude,
"longitude" to longitude,
"accuracy" to accuracy
))
MethodChannel(it, CHANNEL).invokeMethod(
"locationUpdate", mapOf(
"latitude" to latitude,
"longitude" to longitude,
"accuracy" to accuracy
)
)
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
MethodChannel(
flutterEngine!!.dartExecutor.binaryMessenger,
CHANNEL
).setMethodCallHandler { call, result ->
when (call.method) {
"startLocationUpdates" -> {
val interval = (call.argument<Number>("interval")?.toLong()) ?: 60000L
val stopAfterTimestamp = (call.argument<Number>("stopAfterTimestamp")?.toLong()) ?: (System.currentTimeMillis() + 60000L)
val stopAfterTimestamp = (call.argument<Number>("stopAfterTimestamp")?.toLong())
?: (System.currentTimeMillis() + 60000L)
if (!isMyServiceRunning(LocationService::class.java)) {
startService(interval, stopAfterTimestamp)
} else {
Toast.makeText(this, "Location service is already running", Toast.LENGTH_SHORT).show()
Toast.makeText(
this,
"Location service is already running",
Toast.LENGTH_SHORT
).show()
}
result.success(null)
}

"stopLocationUpdates" -> {
stopService()
result.success(null)
}

else -> result.notImplemented()
}
}

// Register the receiver for location updates
// Register the receiver for location updates, with proper export settings for Android 13+
val filter = IntentFilter("LocationUpdate")
registerReceiver(locationReceiver, filter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(locationReceiver, filter, Context.RECEIVER_EXPORTED)
} else {
registerReceiver(locationReceiver, filter)
}
}

override fun onDestroy() {
Expand Down Expand Up @@ -96,7 +112,8 @@ class MainActivity : FlutterActivity() {
val manager = getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
Toast.makeText(this, "Location service is already running", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Location service is already running", Toast.LENGTH_SHORT)
.show()
return true
}
}
Expand Down

0 comments on commit d12b9e0

Please sign in to comment.