Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restart service on device reboot (Android only) #94

Open
Puspharaj opened this issue Jun 26, 2020 · 14 comments
Open

Restart service on device reboot (Android only) #94

Puspharaj opened this issue Jun 26, 2020 · 14 comments

Comments

@Puspharaj
Copy link

`1.Add the boot receive permission to AndroidManifest.xml:

Register BootBroadcastReceiver inside application tag:
<application
...
...
...

<receiver android:name="rekab.app.background_locator.BootBroadcastReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

...
...
...
`

The above said code is not working after i reboot the device. It has no effect.

@mehdok
Copy link
Collaborator

mehdok commented Sep 11, 2020

Hi @Puspharaj
Thank you for opening an issue, and sorry for answering late, I'll investigate the problem.

@DomingoMG
Copy link

The same problem

@resfandiari
Copy link
Contributor

The same problem
After reboot, the application crashes.

FATAL EXCEPTION: main
    Process: driver.mobile.collectero, PID: 2962
    java.lang.RuntimeException: Unable to start receiver rekab.app.background_locator.BootBroadcastReceiver: kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018)
        at android.app.ActivityThread.-wrap18(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String
        at rekab.app.background_locator.BackgroundLocatorPlugin$Companion.startIsolateService(BackgroundLocatorPlugin.kt:72)
        at rekab.app.background_locator.BackgroundLocatorPlugin$Companion.registerAfterBoot(BackgroundLocatorPlugin.kt:189)
        at rekab.app.background_locator.BootBroadcastReceiver.onReceive(BootBroadcastReceiver.kt:11)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011)
        at android.app.ActivityThread.-wrap18(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
 flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.0, on Microsoft Windows [Version 10.0.19042.985], locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Chrome - develop for the web
[✓] Android Studio
[✓] Connected device (3 available)

• No issues found!

Application property

environment:
  sdk: '>=2.12.0 <3.0.0'
dependencies:
  background_locator: ^1.6.0+1-beta
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="driver.mobile.collectero">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

   <application
        android:label="collectero"
        android:icon="@mipmap/ic_launcher">
       <meta-data android:name="com.google.android.geo.API_KEY"
           android:value="YOUR KEY HERE"/>

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

<!--       background location service-->

       <service android:name="rekab.app.background_locator.IsolateHolderService"
           android:permission="android.permission.FOREGROUND_SERVICE"
           android:exported="true"
           android:foregroundServiceType = "location"/>
       <receiver android:name="rekab.app.background_locator.BootBroadcastReceiver"
           android:enabled="true">
           <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
           </intent-filter>
       </receiver>

        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>
buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "driver.mobile.collectero"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

@irwing844
Copy link

Same 😫

@magnuswikhog
Copy link

@resfandiari I tried applying your fix (#297) but it still didn't work (tested on Google Pixel 4a with Android 12). I still get an error message that the app crashed after reboot, and the background service never seems to start. Anything more than this commit needed for a complete fix?

@resfandiari
Copy link
Contributor

@resfandiari I tried applying your fix (#297) but it still didn't work (tested on Google Pixel 4a with Android 12). I still get an error message that the app crashed after reboot, and the background service never seems to start. Anything more than this commit needed for a complete fix?

There is a second problem #263 even though I fixed this one
And this PR #292 fixed a second problem.
Please wait until these PRs #292 #297 are merged into master.

@3wer3wer
Copy link

3wer3wer commented Jan 7, 2022

Are there any news on this topic? Thank you!

mehdok pushed a commit that referenced this issue Jan 8, 2022
@Yukams
Copy link

Yukams commented Apr 20, 2022

@resfandiari still got crashes on phone reboot ?

@resfandiari
Copy link
Contributor

@resfandiari still got crashes on phone reboot ?

yes because this PR #292 has not yet been merged.

@Yukams
Copy link

Yukams commented Apr 21, 2022

@resfandiari I made a working fork of this repo. Any idea where I could share it so everyone could try it and see if they can work with it ?
It's just a compilation of the currently opened pull request but it took me a while so if I could save some time to someone else...

@itssurjya
Copy link

Still crashes are occur Any Updates

@resfandiari
Copy link
Contributor

Still crashes are occur Any Updates

use this
https://pub.dev/packages/background_locator_2

@itssurjya
Copy link

@resfandiari
It only works on an emulator but does not work on real devices...

@resfandiari
Copy link
Contributor

@resfandiari It only works on an emulator but does not work on real devices...

That's not true. It works perfectly, I tested it on real device.
I think your setup has problem https://github.com/Yukams/background_locator_fixed/wiki/Setup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants