bkonyi / FlutterGeofencing

Rough work for Flutter geofencing plugin
BSD 3-Clause "New" or "Revised" License
339 stars 220 forks source link

Unable to create service io.flutter.plugins.geofencing.GeofencingService #19

Closed jawand closed 5 years ago

jawand commented 5 years ago

I added this plugin into my project and when I click the Register button to add a GeoFence it throws this exception.

I am running this on Emulator Pixel 2 API 27. But I get the same error on a real device with Noguet 7.1.

Location is Mode is on High Accuracy and wifi and mobile data are enabled.

This is my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.milton.smartthings">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-feature android:name="android.hardware.wifi" />
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application android:name=".Application" android:label="Milton" android:icon="@mipmap/ic_launcher">
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="API_KEY" />
        <activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <receiver android:name="io.flutter.plugins.geofencing.GeofencingBroadcastReceiver" android:enabled="true" android:exported="true"/>
        <receiver android:name="io.flutter.plugins.geofencing.GeofencingRebootBroadcastReceiver" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
        <service android:name="io.flutter.plugins.geofencing.GeofencingService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
    </application>
</manifest>

this is Application.java

package in.milton.smartthings;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.geofencing.GeofencingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
      super.onCreate();
      GeofencingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
      GeneratedPluginRegistrant.registerWith(registry);
    }
  }
I/GeofencingPlugin( 6681): Successfully added geofence
I/GeofencingService( 6681): Starting GeofencingService...
D/AndroidRuntime( 6681): Shutting down VM
E/AndroidRuntime( 6681): FATAL EXCEPTION: main
E/AndroidRuntime( 6681): Process: in.milton.smartthings, PID: 6681
E/AndroidRuntime( 6681): java.lang.RuntimeException: Unable to create service io.flutter.plugins.geofencing.GeofencingService: java.lang.IllegalStateException: registrar.activity() must not be null
E/AndroidRuntime( 6681):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3349)
E/AndroidRuntime( 6681):    at android.app.ActivityThread.-wrap4(Unknown Source:0)
E/AndroidRuntime( 6681):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677)
E/AndroidRuntime( 6681):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime( 6681):    at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime( 6681):    at android.app.ActivityThread.main(ActivityThread.java:6494)
E/AndroidRuntime( 6681):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 6681):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E/AndroidRuntime( 6681):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
E/AndroidRuntime( 6681): Caused by: java.lang.IllegalStateException: registrar.activity() must not be null
E/AndroidRuntime( 6681):    at com.apptreesoftware.barcodescan.BarcodeScanPlugin$Companion.registerWith(BarcodeScanPlugin.kt:19)
E/AndroidRuntime( 6681):    at com.apptreesoftware.barcodescan.BarcodeScanPlugin.registerWith(Unknown Source:2)
E/AndroidRuntime( 6681):    at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:33)
E/AndroidRuntime( 6681):    at in.milton.smartthings.Application.registerWith(Application.java:18)
E/AndroidRuntime( 6681):    at io.flutter.plugins.geofencing.GeofencingService.startGeofencingService(GeofencingService.kt:73)
E/AndroidRuntime( 6681):    at io.flutter.plugins.geofencing.GeofencingService.onCreate(GeofencingService.kt:113)
E/AndroidRuntime( 6681):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3339)

Any help is appreciated.

bkonyi commented 5 years ago

It looks like you're using another plugin that requires the presence of an Activity on Android and is trying to access it while it's being initialized. This won't work when there's no UI and the app is executing in the background, so you'll need to manually make sure that plugin doesn't get registered. Unfortunately this is a general issue with all background enabled plugins, not just this one.

CedSW commented 4 years ago

for me it was keyboard_visibility 0.5.2, maybe it helps....

marcolettieri commented 4 years ago

I have the same issue, the plugin have to manage this: https://github.com/transistorsoft/flutter_background_fetch/blob/master/android/src/main/java/com/transistorsoft/flutter/backgroundfetch/BackgroundFetchPlugin.java#L46

cahyowhy commented 4 years ago

i clone this repo and run the example.. and tried to click register button and still got this.. this is my environment

[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15 19A603, locale en-ID)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install:
        sudo gem install cocoapods
[!] Android Studio (version 3.6)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.45.1)
[✓] Connected device (1 available)