后台位置中出现 RECEIVER_EXPORTED 或 RECEIVER_NOT_EXPORTED 错误

问题描述 投票:0回答:1

我正在使用带有background_location包的后台服务,但是当我尝试运行该应用程序时, 我因这个错误而崩溃了:

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.

我期待后台位置运行广告给我定期更新位置

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

这里是后台服务代码

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");
  });

}

我在网上尝试了一些解决方案,但没有一个可以帮助:

android flutter geolocation background-process
1个回答
0
投票

您可以像下面这样进行全局设置

  • 创建扩展
    MainApplication.java
     的类 
    MainApplication.kt
    android.app.Application
  • 打开 Android Manifest 并将其添加到
    <application>
    标签,如下所示
<application
        android:name=".MainApplication"
  • 重写此方法或使用此方法
@Override
    public Intent registerReceiver(@Nullable BroadcastReceiver receiver, IntentFilter filter) {
        if (Build.VERSION.SDK_INT >= 34 && getApplicationInfo().targetSdkVersion >= 34) {
            return super.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED);
        } else {
            return super.registerReceiver(receiver, filter);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.