Search code examples
androidfluttergeolocationbackground-process

RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED error in Background Location


I am using background service, with background_location package, but when i try to run the app, I crashed shoeing this error:

D/AndroidRuntime(22315): Shutting down VM
E/AndroidRuntime(22315): FATAL EXCEPTION: main
E/AndroidRuntime(22315): Process: com.throttle.app.throttlemvp, PID: 22315
E/AndroidRuntime(22315): java.lang.RuntimeException: Unable to create service com.almoullim.background_location.LocationUpdatesService: java.lang.SecurityException: com.throttle.app.throttlemvp: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
E/AndroidRuntime(22315):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4878)
E/AndroidRuntime(22315):    at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime(22315):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2365)
E/AndroidRuntime(22315):    at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(22315):    at android.os.Looper.loopOnce(Looper.java:232)
E/AndroidRuntime(22315):    at android.os.Looper.loop(Looper.java:317)
E/AndroidRuntime(22315):    at android.app.ActivityThread.main(ActivityThread.java:8501)
E/AndroidRuntime(22315):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(22315):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/AndroidRuntime(22315):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/AndroidRuntime(22315): Caused by: java.lang.SecurityException: com.throttle.app.throttlemvp: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
E/AndroidRuntime(22315):    at android.os.Parcel.createExceptionOrNull(Parcel.java:3182)
E/AndroidRuntime(22315):    at android.os.Parcel.createException(Parcel.java:3166)
E/AndroidRuntime(22315):    at android.os.Parcel.readException(Parcel.java:3149)
E/AndroidRuntime(22315):    at android.os.Parcel.readException(Parcel.java:3091)
E/AndroidRuntime(22315):    at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5784)
E/AndroidRuntime(22315):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1853)
E/AndroidRuntime(22315):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1793)
E/AndroidRuntime(22315):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1781)
E/AndroidRuntime(22315):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:756)
E/AndroidRuntime(22315):    at com.almoullim.background_location.LocationUpdatesService.onCreate(LocationUpdatesService.kt:154)
E/AndroidRuntime(22315):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4865)
E/AndroidRuntime(22315):    ... 9 more
E/AndroidRuntime(22315): Caused by: android.os.RemoteException: Remote stack trace:
E/AndroidRuntime(22315):    at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:14337)
E/AndroidRuntime(22315):    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2615)
E/AndroidRuntime(22315):    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2798)
E/AndroidRuntime(22315):    at android.os.Binder.execTransactInternal(Binder.java:1496)
E/AndroidRuntime(22315):    at android.os.Binder.execTransact(Binder.java:1440)
E/AndroidRuntime(22315): 
Lost connection to device.

I was expecting the background location to run ad give me periodic updates on location

////////////////////////////////////////////////////////////////////////////////////////////////

here is the background service code

import 'dart:async';

import 'package:background_location/background_location.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
//import 'package:location/location.dart' ;
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_background_service_android/flutter_background_service_android.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import 'globalVariables.dart';

final service = FlutterBackgroundService();
LatLng? _pUserLoc;


Future<void> getLocationUpdates() async{
  WidgetsFlutterBinding.ensureInitialized();

  BackgroundLocation.setAndroidConfiguration(1000);
  BackgroundLocation.stopLocationService();
  BackgroundLocation.startLocationService();


  BackgroundLocation.getLocationUpdates((currentLocation) {
    if(currentLocation.latitude != null &&
        currentLocation.longitude != null){
      _pUserLoc = LatLng(currentLocation.latitude!, currentLocation.longitude!);
      updateDatabaseRide(_pUserLoc!);
      if (kDebugMode) {
        print(_pUserLoc);
      }
  }});

}

Future<void> updateDatabaseRide(LatLng pos) async{
  DatabaseReference ref = FirebaseDatabase.instance.ref("bikers/$rideCode/$userName");

  await ref.update({
    "lat": pos.latitude,
    "lng": pos.longitude,
  });
}

Future<void> initializeService() async{
  await service.configure(
      iosConfiguration: IosConfiguration(
        autoStart: true,
        onForeground: onStart,
        onBackground: onIosBackground,
      ),
      androidConfiguration: AndroidConfiguration(
          onStart: onStart,
          isForegroundMode: true,
          autoStart: true
      )
  );
}

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async{
  WidgetsFlutterBinding.ensureInitialized();
  //DartPluginRegistrant.ensureInitialized();
  return true;
}

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async{
  //DartPluginRegistrant.ensureInitialized();
  if(service is AndroidServiceInstance){
    service.on('setAsForeground').listen((event) {service.setAsForegroundService();});
  }
  service.on('stopService').listen((event) {service.stopSelf();});


  Timer.periodic(const Duration(seconds: 1),(timer) async{
    if(service is AndroidServiceInstance){
      if(await service.isForegroundService()){
        service.setForegroundNotificationInfo(title: "Foreground Notification", content: "If you see this, your phone is super sensitive");
      }
    }
    getLocationUpdates();
    if (kDebugMode) {
      print("background service running");
    }
    service.invoke("update");
  });

}

I tried a few solutions online, but none could help:


Solution

  • enter image description here

    I am using the background_location package and I faced this problem

    background_location:
    
       git:
        url: https://github.com/dharmik-dalwadi-seaflux/background_location.git
        ref: master
    

    use this so that you can over come your problem