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

Wakelock.enable() does not work on Android #160

Closed
rohitvishwakarma1819 opened this issue Jan 1, 2022 · 19 comments · Fixed by #168
Closed

Wakelock.enable() does not work on Android #160

rohitvishwakarma1819 opened this issue Jan 1, 2022 · 19 comments · Fixed by #168
Labels
requires further investigation The root cause of the issue has not yet been fully identified waiting for response The OP needs to respond

Comments

@rohitvishwakarma1819
Copy link

I just created a bank flutter application and added
Wakelock.enable(); as the first line inside main() method.
I doesn't prevent my app from going to sleep.

@creativecreatorormaybenot
Copy link
Owner

Hi, thanks for using the package! Without further detail, it will be impossible to help you here. The package does work for most users after all.

Which phone are you using?

@creativecreatorormaybenot creativecreatorormaybenot added requires further investigation The root cause of the issue has not yet been fully identified waiting for response The OP needs to respond labels Jan 1, 2022
@rohitvishwakarma1819
Copy link
Author

rohitvishwakarma1819 commented Jan 1, 2022

@creativecreatorormaybenot
I'm using moto G40 fusion (arm-64) with Anfroid11

@creativecreatorormaybenot
Copy link
Owner

@rohitvishwakarma1819 I do not have this device here to test. Can you test on a different device and confirm that the wakelock is working, i.e. that you are using it properly?

@itssidhere
Copy link

Same. Doesn't work with Android 11

@creativecreatorormaybenot
Copy link
Owner

@itssidhere hi - thanks for reporting. I have the same request for you, i.e. sharing how you are using it / testing with different devices for reproduction ✌️

@itssidhere
Copy link

@creativecreatorormaybenot seems like it's the issue with latest flutter version 2.9 can't be sure though I am using poco X3 Pro and yes it doesn't work on any of my devices

@creativecreatorormaybenot
Copy link
Owner

@itssidhere I checked again on my devices and I cannot reproduce.

Without further input, I cannot help you here - unfortuantely. You will at least have to either debug the issue yourself and figure out where it comes from or post a reproducible sample. My advice would be to use the example app for testing this.

@paj-co
Copy link

paj-co commented Jan 11, 2022

I have tested wakelock ^0.6.0+1 on Samsung A21s with Android 11. I'm using Flutter 2.8.1 stable, everything is working fine 👍

@itssidhere
Copy link

@creativecreatorormaybenot I agree I might have to look in detail. I will do so and update you. Thanks.

@creativecreatorormaybenot creativecreatorormaybenot changed the title Wakelock.enable(); doesn't work Wakelock.enable() does not work on Android Jan 11, 2022
@scognito
Copy link

Doens't work on my Android 11 Xiaomi 10 lite 5g.
Flutter version 2.8.1
Available for testing if you tell me how to do.

@A2-NieR
Copy link

A2-NieR commented Jan 23, 2022

Same here, using a Moto G7 Plus, tho with Lineage OS 18.1 which is based on Android 11.

Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 77d935af4d (5 weeks ago) • 2021-12-16 08:37:33 -0800
Engine • revision 890a5fca2e
Tools • Dart 2.15.1

Also, just to make sure I did not use it incorrectly? 😅

import 'package:flutter/material.dart';
import 'package:wakelock/wakelock.dart';

void main() {
  Wakelock.enable();

  runApp(MaterialApp(
    initialRoute: '/home',
    routes: {
      ...
    },
  ));
}

@creativecreatorormaybenot
Copy link
Owner

@bunnythelifeguard Can you try calling Wakelock.enable more often? As in make sure that it is not released during the app lifecycle?
You could even call it in build if you wanted to

If that does not fix it, there definitely is an issue with the plugin

@A2-NieR
Copy link

A2-NieR commented Jan 24, 2022

@creativecreatorormaybenot looks like calling it inside a build took care of it, device did not go to sleep so far.

@override
  Widget build(BuildContext context) {
    Wakelock.enable();

    return const Scaffold(
      body: Text("loading"),
    );
  }

Where would the most central/earliest opportunity be to call it, or does one have to call it inside every build? (Still learning the framework ^^)

@creativecreatorormaybenot
Copy link
Owner

@bunnythelifeguard Calling it inside of build is mostly arbitrary. What I think might be the case is that either you are calling Wakelock.disable() somewhere or the wakelock is released by another source.

A third option that I see is the following:

void main() {
  Wakelock.enable();

  runApp(..);
}

Will not work. That is because the method channels have not yet been initialized.
You either have to call it after runApp or initialize the widgets binding beforehand:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Wakelock.enable();

  runApp(..);
}

@A2-NieR
Copy link

A2-NieR commented Jan 25, 2022

@creativecreatorormaybenot Thanks for explaining, brother 🙂
Intersting, I have to read more about the WidgetsFlutterBinding.ensureInitialized(); 👀

Just as an fyi: I did not call anything besides Wakelock.enable(); so you can rule out option 1. WL being released by another source I can't tell since I'm not experienced enough overall with flutter yet. I was using it during/in a simple tutorial project, so nothing too complex. Hope that helps.

@pdivita
Copy link

pdivita commented Jan 25, 2022

Worked for me, I suggest to add in the README.
BTW I'm using version 0.5.6 because of this issue.

@bunnythelifeguard Calling it inside of build is mostly arbitrary. What I think might be the case is that either you are calling Wakelock.disable() somewhere or the wakelock is released by another source.

A third option that I see is the following:

void main() {
  Wakelock.enable();

  runApp(..);
}

Will not work. That is because the method channels have not yet been initialized. You either have to call it after runApp or initialize the widgets binding beforehand:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Wakelock.enable();

  runApp(..);
}

@creativecreatorormaybenot
Copy link
Owner

Worked for me, I suggest to add in the README.

That is great to hear @pdivita! Which fix are you referring to?

@A2-NieR
Copy link

A2-NieR commented Jan 25, 2022

In my case in a new project both worked.
Tho I had to upgrade Kotlin version in build.gradle, since it is only created with 1.3.50.

@pdivita
Copy link

pdivita commented Jan 26, 2022

Worked for me, I suggest to add in the README.

That is great to hear @pdivita! Which fix are you referring to?

Wakelock started working after I've added WidgetsFlutterBinding.ensureInitialized(); before the call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
requires further investigation The root cause of the issue has not yet been fully identified waiting for response The OP needs to respond
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants