位置更新接收器未接收到位置更新 android

huangapple go评论134阅读模式
英文:

Location update Receiver not getting location updates android

问题

我已使用PendingIntent启动广播接收器以获取位置更新。应用在之前运行正常,但在过去几个月停止工作。我检查了发现LocationReceiver.kt没有收到任何更新。

LocationReceiver.kt 代码如下:

  1. class LocationReceiver : BroadcastReceiver() {
  2. private var locationRepository: LocationRepository? = null
  3. private var lastLocation: Location? = null
  4. override fun onReceive(context: Context, intent: Intent) {
  5. val action = intent.action
  6. if (Utility.currentApiVersion >= Build.VERSION_CODES.O) NotificationHandler.getInstance(context).updateNotification(action)
  7. Log.d("locationslog", "" + action)
  8. Utility.appToastShort(context, "onReceive: " + action)
  9. if (ACTION_LOCATION_UPDATES == action) {
  10. val result = LocationResult.extractResult(intent)
  11. if (result != null) {
  12. val locations = result.locations
  13. if (locationRepository == null) locationRepository = LocationRepository.getInstance(context.applicationContext, Executors.newSingleThreadExecutor())
  14. if (locations.size > 0) {
  15. if (locations.size == 1 && lastLocation != null && lastLocation!!.latitude == locations[0].latitude && lastLocation!!.latitude == locations[0].longitude) {
  16. //already saved location
  17. } else {
  18. for (i in locations.indices) {
  19. if (i == locations.size - 1) lastLocation = locations[i]
  20. val curntLoc = locations[i]
  21. locationRepository!!.insertLocation(
  22. LocationPoint(
  23. curntLoc.latitude,
  24. curntLoc.longitude
  25. )
  26. )
  27. }
  28. if (Utility.currentApiVersion >= Build.VERSION_CODES.O) NotificationHandler.getInstance(context).updateNotification(
  29. Utils.getLocationText(lastLocation)
  30. )
  31. }
  32. }
  33. }
  34. }
  35. }
  36. companion object {
  37. const val ACTION_LOCATION_UPDATES = "ACTION_LOCATION_UPDATE"
  38. }
  39. }

LocationUpdatesService.java 代码如下:

  1. public class LocationUpdatesService extends Service {
  2. private static final String PACKAGE_NAME = "dk.invoiceportal.mileage";
  3. public static final String EXTRA_LOCATION = PACKAGE_NAME + ".location";
  4. public static final String LAST_LOCATION = PACKAGE_NAME + ".lastlocation";
  5. public static final String STOP_LOCATION = PACKAGE_NAME + ".stop";
  6. private static final String TAG = LocationUpdatesService.class.getSimpleName();
  7. private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME + ".started_from_notification";
  8. private static final int NOTIFICATION_ID = 2123;
  9. private final IBinder mBinder = new LocalBinder();
  10. private boolean mChangingConfiguration = false, tracking = false;
  11. private Location mLocation;
  12. private Context mContext;
  13. private Activity activity;
  14. private LocationRepository locationRepository;
  15. private Thread thread;
  16. private NotificationHandler notificationHandler = null;
  17. public LocationUpdatesService() {
  18. }
  19. @RequiresApi(api = Build.VERSION_CODES.O)
  20. @Override
  21. public void onCreate() {
  22. getLocationRepository();
  23. notificationHandler = NotificationHandler.getInstance(this);
  24. }
  25. // 其余部分请查看您的代码。
  26. }

LocationHandler.kt 代码如下:

  1. class LocationHandler private constructor(private val context: Context ) {
  2. private var mLocationRequest: LocationRequest? = null
  3. private var mFusedLocationClient: FusedLocationProviderClient? = null
  4. private val _receivingLocationUpdates: MutableLiveData<Boolean> = MutableLiveData<Boolean>(false)
  5. // 其余部分请查看您的代码。
  6. }

LocationRepository.kt 代码如下:

  1. class LocationRepository private constructor(
  2. private val myDataBase: MyLocationDatabase,
  3. private val locationHandler: LocationHandler,
  4. private val executor: ExecutorService
  5. ) {
  6. // 数据库相关字段/方法
  7. // 其余部分请查看您的代码。
  8. }

请注意,这只是您提供的代码的部分翻译,而不包括完整的内容。如果需要更多翻译,请提供更多的代码部分。

英文:

i have used pending intent to launch broadcast receiver get location updates.

app was working fine before.

but from last couple of months it stopped working.

i checked and found LocationReceiver.kt is not getting any updates

  1. class LocationReceiver : BroadcastReceiver() {
  2. private var locationRepository: LocationRepository? = null
  3. private var lastLocation: Location? = null
  4. override fun onReceive(context: Context, intent: Intent) {
  5. val action = intent.action
  6. if (Utility.currentApiVersion \&gt;= Build.VERSION_CODES.O) NotificationHandler.getInstance(context).updateNotification(action)
  7. Log.d(&quot;locationslog&quot;,&quot;&quot;+action)
  8. Utility.appToastShort(context, &quot;onReceive: &quot;+action)
  9. if (ACTION_LOCATION_UPDATES == action) {
  10. val result = LocationResult.extractResult(intent)
  11. if (result != null) {
  12. val locations = result.locations
  13. if (locationRepository == null) locationRepository =
  14. LocationRepository.getInstance(context.applicationContext, Executors.newSingleThreadExecutor())
  15. if (locations.size \&gt; 0) {
  16. if (locations.size == 1 &amp;&amp; lastLocation != null &amp;&amp; lastLocation!!.latitude == locations\[0\].latitude &amp;&amp; lastLocation!!.latitude == locations\[0\].longitude) {
  17. //already saved location
  18. } else {
  19. for (i in locations.indices) {
  20. if (i == locations.size - 1) lastLocation = locations\[i\]
  21. val curntLoc = locations\[i\]
  22. //LatLng curntLatLng = new LatLng(curntLoc.getLatitude(), curntLoc.getLongitude());
  23. locationRepository!!.insertLocation(
  24. LocationPoint(
  25. curntLoc.latitude,
  26. curntLoc.longitude
  27. )
  28. )
  29. }
  30. if (Utility.currentApiVersion \&gt;= Build.VERSION_CODES.O) NotificationHandler.getInstance( context).updateNotification(
  31. Utils.getLocationText(lastLocation)
  32. )
  33. }
  34. }
  35. }
  36. }
  37. }
  38. companion object {
  39. const val ACTION_LOCATION_UPDATES = &quot;ACTION_LOCATION_UPDATE&quot;
  40. }
  41. }
  1. public class LocationUpdatesService extends Service {
  2. private static final String PACKAGE_NAME = &quot;dk.invoiceportal.mileage&quot;;
  3. public static final String EXTRA_LOCATION = PACKAGE_NAME + &quot;.location&quot;;
  4. public static final String LAST_LOCATION = PACKAGE_NAME + &quot;.lastlocation&quot;;
  5. public static final String STOP_LOCATION = PACKAGE_NAME + &quot;.stop&quot;;
  6. private static final String TAG = LocationUpdatesService.class.getSimpleName();
  7. private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME + &quot;.started_from_notification&quot;;
  8. private static final int NOTIFICATION_ID = 2123;
  9. private final IBinder mBinder = new LocalBinder();
  10. private boolean mChangingConfiguration = false, tracking = false;
  11. private Location mLocation;
  12. private Context mContext;
  13. private Activity activity;
  14. private LocationRepository locationRepository;
  15. private Thread thread;
  16. private NotificationHandler notificationHandler = null;
  17. public LocationUpdatesService() {
  18. }
  19. @RequiresApi(api = Build.VERSION_CODES.O)
  20. @Override
  21. public void onCreate() {
  22. getLocationRepository();
  23. notificationHandler = NotificationHandler.getInstance(this);
  24. }
  25. @RequiresApi(api = Build.VERSION_CODES.O)
  26. @Override
  27. public int onStartCommand(Intent intent, int flags, int startId) {
  28. Log.i(TAG, &quot;Service started&quot;);
  29. boolean startedFromNotification = false;
  30. if (intent != null)
  31. startedFromNotification = intent.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
  32. false);
  33. // We got here because the user decided to remove location updates from the notification.
  34. if (startedFromNotification) {
  35. removeLocationUpdates();
  36. stopSelf();
  37. } else {
  38. getLocationRepository();
  39. if (notificationHandler == null)
  40. notificationHandler = NotificationHandler.getInstance(this);
  41. notificationHandler.activity = activity;
  42. startForeground(NOTIFICATION_ID, notificationHandler.getNotification(Utils.getLocationText(mLocation)));
  43. }
  44. // Tells the system to not try to recreate the service after it has been killed.
  45. return START_STICKY;
  46. }
  47. private void startWorkOnNewThread(GoogleMap mGoogleMap, final int start, final String event) {
  48. if (start == 1)
  49. locationRepository.startLocationUpdates();
  50. else if (start == 0) {
  51. locationRepository.stopLocationUpdates();
  52. tracking = false;
  53. if (notificationHandler != null)
  54. notificationHandler.cancelNotification();
  55. if (event.equalsIgnoreCase(&quot;stopself&quot;))
  56. stopSelf();
  57. } else if (start == 2)
  58. locationRepository.getLastLocation(mGoogleMap, event);
  59. stopThread();
  60. /* thread = new Thread(new Runnable() {
  61. public void run() {
  62. if(start == 1)
  63. locationRepository.startLocationUpdates(getApplicationContext());
  64. else if(start == 0)
  65. {
  66. locationRepository.stopLocationUpdates(getApplicationContext());
  67. if(event.equalsIgnoreCase(&quot;stopself&quot;))
  68. stopSelf();
  69. }
  70. else if(start ==2)
  71. locationRepository.getLastLocation(event);
  72. }
  73. });
  74. thread.start();*/
  75. }
  76. @RequiresApi(api = Build.VERSION_CODES.O)
  77. @Override
  78. public void onTaskRemoved(Intent rootIntent) {
  79. //When remove app from background then start it again
  80. if (tracking)
  81. startForegroundService(new Intent(getApplicationContext(), LocationUpdatesService.class));
  82. super.onTaskRemoved(rootIntent);
  83. }
  84. @Override
  85. public void onConfigurationChanged(Configuration newConfig) {
  86. super.onConfigurationChanged(newConfig);
  87. mChangingConfiguration = true;
  88. }
  89. @Override
  90. public IBinder onBind(Intent intent) {
  91. // Called when a client (MapActivity in case of this sample) comes to the foreground
  92. // and binds with this service. The service should cease to be a foreground service
  93. // when that happens.
  94. Log.i(TAG, &quot;in onBind()&quot;);
  95. stopForeground(true);
  96. mChangingConfiguration = false;
  97. return mBinder;
  98. }
  99. @Override
  100. public void onRebind(Intent intent) {
  101. // Called when a client (MapActivity in case of this sample) returns to the foreground
  102. // and binds once again with this service. The service should cease to be a foreground
  103. // service when that happens.
  104. Log.i(TAG, &quot;in onRebind()&quot;);
  105. stopForeground(true);
  106. mChangingConfiguration = false;
  107. super.onRebind(intent);
  108. }
  109. @RequiresApi(api = Build.VERSION_CODES.O)
  110. @Override
  111. public boolean onUnbind(Intent intent) {
  112. Log.i(TAG, &quot;Last client unbound from service&quot;);
  113. // Called when the last client (MapActivity in case of this sample) unbinds from this
  114. // service. If this method is called due to a configuration change in MapActivity, we
  115. // do nothing. Otherwise, we make this service a foreground service.
  116. if (!mChangingConfiguration &amp;&amp; Utils.requestingLocationUpdates(this)) {
  117. Log.i(TAG, &quot;Starting foreground service&quot;);
  118. if (notificationHandler == null) {
  119. notificationHandler = NotificationHandler.getInstance(this);
  120. notificationHandler.setNotificationManager();
  121. }
  122. notificationHandler.activity = activity;
  123. startForeground(NOTIFICATION_ID, notificationHandler.getNotification(Utils.getLocationText(mLocation)));
  124. }
  125. return true; // Ensures onRebind() is called when a client re-binds.
  126. }
  127. @RequiresApi(api = Build.VERSION_CODES.O)
  128. public void requestLocationUpdates() {
  129. Log.i(TAG, &quot;Requesting location updates&quot;);
  130. tracking = true;
  131. startForegroundService(new Intent(getApplicationContext(), LocationUpdatesService.class));
  132. try {
  133. startWorkOnNewThread(null, 1, &quot;&quot;);
  134. } catch (SecurityException unlikely) {
  135. Utils.setRequestingLocationUpdates(this, false);
  136. Log.e(TAG, &quot;Lost location permission. Could not request updates. &quot; + unlikely);
  137. }
  138. }
  139. public void getLastLocation(GoogleMap mGoogleMap, String event) {
  140. try {
  141. startWorkOnNewThread(mGoogleMap, 2, event);
  142. } catch (SecurityException unlikely) {
  143. Utils.setRequestingLocationUpdates(this, false);
  144. Log.e(TAG, &quot;Lost location permission. Could not remove updates. &quot; + unlikely);
  145. }
  146. }
  147. public void removeLocationUpdates() {
  148. Log.i(TAG, &quot;Removing location updates&quot;);
  149. try {
  150. startWorkOnNewThread(null, 0, &quot;stopself&quot;);
  151. //stopSelf();
  152. } catch (SecurityException unlikely) {
  153. Utils.setRequestingLocationUpdates(this, true);
  154. Log.e(TAG, &quot;Lost location permission. Could not remove updates. &quot; + unlikely);
  155. }
  156. }
  157. private LocationRepository getLocationRepository() {
  158. if (locationRepository == null)
  159. locationRepository = LocationRepository.Companion.getInstance(getApplicationContext(), Executors.newSingleThreadExecutor());
  160. return locationRepository;
  161. }
  162. private void stopThread() {
  163. if (thread != null)
  164. thread.interrupt();
  165. thread = null;
  166. }
  167. @Override
  168. public void onDestroy() {
  169. Utils.setRequestingLocationUpdates(this, false);
  170. if (notificationHandler != null)
  171. notificationHandler.destroy();
  172. notificationHandler = null;
  173. stopThread();
  174. }
  175. public class LocalBinder extends Binder {
  176. public LocationUpdatesService getService(Context context) {
  177. mContext = context;
  178. activity = (Activity) context;
  179. return LocationUpdatesService.this;
  180. }
  181. }
  182. }
  1. class LocationHandler private constructor(private val context: Context ) {
  2. private var mLocationRequest: LocationRequest? = null
  3. private var mFusedLocationClient: FusedLocationProviderClient? = null
  4. private val _receivingLocationUpdates: MutableLiveData&lt;Boolean&gt; = MutableLiveData&lt;Boolean&gt;(false)
  5. val receivingLocationUpdates: LiveData&lt;Boolean&gt;
  6. get() = _receivingLocationUpdates
  7. @SuppressLint(&quot;MissingPermission&quot;)
  8. fun startLocationUpdates() {
  9. if (!AppPermissions.checkPermissions(context, true)) {
  10. Utility.appToast(context, &quot;Location permission not given or GPS is OFF&quot;)
  11. return
  12. } else {
  13. _receivingLocationUpdates.value = true
  14. if (mLocationRequest == null) createLocationRequest()
  15. Utils.setRequestingLocationUpdates(context, true)
  16. if (mFusedLocationClient == null) mFusedLocationClient =
  17. LocationServices.getFusedLocationProviderClient(
  18. context
  19. )
  20. mFusedLocationClient!!.requestLocationUpdates(mLocationRequest!!, pendingIntent)
  21. Log.d(&quot;locationslog&quot;,&quot;startLocationUpdates&quot;)
  22. Utility.appToastShort(context, &quot;startLocationUpdates&quot;)
  23. }
  24. }
  25. fun stopLocationUpdate() {
  26. if (mLocationRequest == null) createLocationRequest()
  27. if ( mFusedLocationClient != null) {
  28. mFusedLocationClient!!.removeLocationUpdates(pendingIntent)
  29. }
  30. Log.d(&quot;locationslog&quot;,&quot;stopLocationUpdate&quot;)
  31. Utils.setRequestingLocationUpdates(context, false)
  32. _receivingLocationUpdates.value = false
  33. Utility.appToastShort(context, &quot;stopLocationUpdate&quot;)
  34. }
  35. private fun createLocationRequest() {
  36. mLocationRequest = LocationRequest.Builder(
  37. Priority.PRIORITY_HIGH_ACCURACY,
  38. UPDATE_INTERVAL_IN_MILLISECONDS
  39. )
  40. .setMinUpdateDistanceMeters(SMALLEST_DISTANCE_IN_METER)
  41. .setIntervalMillis(UPDATE_INTERVAL_IN_MILLISECONDS)
  42. .setMinUpdateIntervalMillis(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS)
  43. .setMaxUpdateDelayMillis(MAX_WAIT_TIME).setWaitForAccurateLocation(false)
  44. .setPriority(Priority.PRIORITY_HIGH_ACCURACY).build()
  45. displayGpsMessage()
  46. }
  47. // Note: for apps targeting API level 25 (&quot;Nougat&quot;) or lower, either
  48. // PendingIntent.getService() or PendingIntent.getBroadcast() may be used when requesting
  49. // location updates. For apps targeting API level O, only
  50. // PendingIntent.getBroadcast() should be used. This is due to the limits placed on services
  51. // started in the background in &quot;O&quot;.
  52. private val pendingIntent: PendingIntent
  53. get() {
  54. // Note: for apps targeting API level 25 (&quot;Nougat&quot;) or lower, either
  55. // PendingIntent.getService() or PendingIntent.getBroadcast() may be used when requesting
  56. // location updates. For apps targeting API level O, only
  57. // PendingIntent.getBroadcast() should be used. This is due to the limits placed on services
  58. // started in the background in &quot;O&quot;.
  59. val intent = Intent(LocationReceiver.ACTION_LOCATION_UPDATES)
  60. //intent.action = LocationReceiver.ACTION_LOCATION_UPDATES
  61. Utility.appToastShort(context, &quot;intent:&quot;+intent.action)
  62. Log.d(&quot;locationslog&quot;,&quot;&quot;+intent.action)
  63. var p_intent = PendingIntent.getBroadcast(
  64. context,
  65. 32,
  66. intent,
  67. PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
  68. )
  69. p_intent.send();
  70. return p_intent;
  71. }
  72. fun getLastLocation(mGoogleMap: GoogleMap?, event: String) {
  73. try {
  74. if (mLocationRequest == null) createLocationRequest()
  75. if (mFusedLocationClient == null) mFusedLocationClient =
  76. LocationServices.getFusedLocationProviderClient(
  77. context
  78. )
  79. // _receivingLocationUpdates = true;
  80. Utils.setRequestingLocationUpdates(context, true)
  81. mFusedLocationClient!!.lastLocation
  82. .addOnCompleteListener { task -&gt;
  83. if (task.isSuccessful &amp;&amp; task.result != null) {
  84. val mLocation = task.result
  85. if (mLocation != null) {
  86. if (event.equals(
  87. LocationUpdatesService.LAST_LOCATION,
  88. ignoreCase = true
  89. )
  90. ) {
  91. if (mGoogleMap != null) Utility.setMapCammera(
  92. mGoogleMap,
  93. LatLng(mLocation.latitude, mLocation.longitude)
  94. )
  95. } else {
  96. val loc = LocationPoint(mLocation.latitude, mLocation.longitude)
  97. LocationRepository.getInstance(
  98. context.applicationContext,
  99. Executors.newSingleThreadExecutor()
  100. ).insertLocation(loc)
  101. if (Utility.currentApiVersion &gt;= Build.VERSION_CODES.O) NotificationHandler.getInstance(
  102. context
  103. ).updateNotification(Utils.getLocationText(mLocation))
  104. }
  105. stopLocationUpdate()
  106. } else Log.i(&quot;TAG&quot;, &quot;Last location not null&quot;)
  107. } else Log.i(&quot;TAG&quot;, &quot;Last location not received&quot;)
  108. }.addOnFailureListener { e -&gt;
  109. e.printStackTrace()
  110. Log.i(&quot;TAG&quot;, e.message!!)
  111. stopLocationUpdate()
  112. }
  113. } catch (unlikely: SecurityException) {
  114. Log.e(&quot;TAG&quot;, &quot;Lost location permission.$unlikely&quot;)
  115. }
  116. }
  117. fun displayGpsMessage() {
  118. if ( mLocationRequest != null) {
  119. val builder = LocationSettingsRequest.Builder()
  120. builder.addLocationRequest(mLocationRequest!!)
  121. val locationSettingsRequest = builder.build()
  122. val settingsClient = LocationServices.getSettingsClient(context)
  123. settingsClient.checkLocationSettings(locationSettingsRequest).addOnFailureListener(Utility.curentActivity
  124. ) { e -&gt;
  125. val statusCode = (e as ApiException).statusCode
  126. when (statusCode) {
  127. LocationSettingsStatusCodes.RESOLUTION_REQUIRED -&gt; try {
  128. // Show the dialog by calling startResolutionForResult(), and check the
  129. // result in onActivityResult().
  130. val rae = e as ResolvableApiException
  131. rae.startResolutionForResult(Utility.curentActivity , 1000)
  132. } catch (sie: SendIntentException) {
  133. Log.i(&quot;location&quot;, &quot;PendingIntent unable to execute request.&quot;)
  134. }
  135. LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -&gt; {
  136. val errorMessage = &quot;Location settings are inadequate, and cannot be &quot; +
  137. &quot;fixed here. Fix in Settings.&quot;
  138. Log.e(&quot;location&quot;, errorMessage)
  139. }
  140. }
  141. }
  142. }
  143. }
  144. companion object {
  145. private const val SMALLEST_DISTANCE_IN_METER = 5f//35f
  146. private const val UPDATE_INTERVAL_IN_MILLISECONDS = (9 * 1000).toLong()
  147. //private final Context mContext;
  148. private const val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = (1 * 1000).toLong()
  149. private const val MAX_WAIT_TIME = (6000 * 3).toLong()//(60000 * 3).toLong()
  150. @Volatile private var INSTANCE: LocationHandler? = null
  151. fun getInstance(context: Context): LocationHandler {
  152. return INSTANCE ?: synchronized(this) {
  153. INSTANCE ?: LocationHandler(context).also { INSTANCE = it }
  154. }
  155. }
  156. }
  157. }
  1. class LocationRepository private constructor(
  2. private val myDataBase: MyLocationDatabase,
  3. private val locationHandler: LocationHandler,
  4. private val executor: ExecutorService
  5. ) {
  6. // Database related fields/methods:
  7. private val locationDao = myDataBase.locationDao()
  8. fun insertLocation(location: LocationPoint?) {
  9. executor.execute { locationDao.insertLocation(location) }
  10. }
  11. fun fetchAllLocation(): LiveData&lt;List&lt;LocationPoint?&gt;?&gt;? {
  12. return locationDao.fetchAllLocation()
  13. }
  14. fun fetchAllLAddresses(): LiveData&lt;List&lt;SavedAddress?&gt;?&gt;? {
  15. return locationDao.fetchAllLAddresses()
  16. }
  17. fun insertSaveAddress(savedAddress: SavedAddress?) {
  18. executor.execute { locationDao.insertAddress(savedAddress) }
  19. }
  20. val locationsCount: Unit
  21. get() {
  22. executor.execute { locationDao.locationsCount }
  23. }
  24. val savedAddressCount: Unit
  25. get() {
  26. executor.execute { locationDao.savedAddressCount }
  27. }
  28. fun deleteLocation(locationPoint: LocationPoint?) {
  29. executor.execute { locationDao.deleteLocation(locationPoint) }
  30. }
  31. fun deleteSavedAddress(savedAddress: SavedAddress?) {
  32. executor.execute {locationDao.deleteSavedAddress(savedAddress) }
  33. }
  34. fun deleteSavedAddress(id: Int) {
  35. executor.execute { locationDao.deleteSavedAddress(id) }
  36. }
  37. fun deleteSavedAddress(address: String?) {
  38. executor.execute { locationDao.deleteSavedAddress(address) }
  39. }
  40. fun deleteAll() {
  41. executor.execute { locationDao.delete() }
  42. }
  43. fun getLastLocation(mGoogleMap: GoogleMap?, event: String?) { locationHandler.getLastLocation(mGoogleMap, event!!)
  44. }
  45. fun startLocationUpdates() {locationHandler.startLocationUpdates()
  46. }
  47. val receivingLocationUpdates: LiveData&lt;Boolean&gt; = locationHandler.receivingLocationUpdates
  48. @MainThread
  49. fun stopLocationUpdates() {
  50. locationHandler.stopLocationUpdate()
  51. }
  52. companion object {
  53. @Volatile private var INSTANCE: LocationRepository? = null
  54. fun getInstance(context: Context, executor: ExecutorService): LocationRepository {
  55. return INSTANCE ?: synchronized(this) {
  56. INSTANCE ?: LocationRepository(
  57. MyLocationDatabase.getInstance(context),
  58. LocationHandler.getInstance(context),executor)
  59. .also { INSTANCE = it }
  60. }
  61. }
  62. }
  63. }

Manifest file

  1. &lt;service
  2. android:name=&quot;.services.LocationUpdatesService&quot;
  3. android:enabled=&quot;true&quot;
  4. android:exported=&quot;true&quot;
  5. android:foregroundServiceType=&quot;location&quot; /\&gt;
  6. &lt;receiver
  7. android:name=&quot;.services.LocationReceiver&quot;
  8. android:enabled=&quot;true&quot;
  9. android:exported=&quot;true&quot;
  10. android:permission=&quot;android.permission.ACCESS_BACKGROUND_LOCATION&quot;&gt;
  11. &lt;intent-filter&gt;
  12. &lt;action android:name=&quot;ACTION_LOCATION_UPDATE&quot; /&gt;
  13. &lt;/intent-filter&gt;
  14. &lt;/receiver&gt;

i have updated the libraries gradle and target sdk is 33. minumm is 24

答案1

得分: 0

将以下内容翻译为中文:

  1. PendingIntent.getBroadcast(context, 32, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

更改为

  1. PendingIntent.getBroadcast(context, 32, intent, PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
英文:

changed
<pre>
PendingIntent.getBroadcast(context,32,intent,<b>PendingIntent.FLAG_IMMUTABLE</b> or PendingIntent.FLAG_UPDATE_CURRENT)
</pre>
to
<pre>
PendingIntent.getBroadcast(context,32,intent,<b>PendingIntent.FLAG_MUTABLE</b> or PendingIntent.FLAG_UPDATE_CURRENT)
</pre>

huangapple
  • 本文由 发表于 2023年3月8日 19:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定