• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with phone state listener inside foreground service

 
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I would like to create the classic Do Not Disturb App with the purpose of silencing notifications and ringtones at times specified by the user. The user could also choose a white list of contacts whose calls should not be silenced.

I tried to create a foreground service in which to insert a phone state listener. Below is a test code. At the moment, when the service is active, the volume is raised for any incoming call. When the device stops ringing, the volume is lowered again.

I tried the code in a physical device with Android 8. There are no problems when debugging with ADB connected. Instead, when I disconnect the device and try to call it, after some attempts the volume is no longer raised (even if the notification of the foreground service is always active).

Manifest permissions:

Starting the service from the Activity (after having requested the previous permissions at runtime):

Service class:

Utility methods to mute/unmute sounds:

I also tried to add the request to put the app in the white list to ignore the battery optimizations (doze mode) but does not change anything.
I did a lot of trials and changes but I just can not figure out where the problem is. The strange thing is that when I try to call the device (after a few minutes that has entered standby), the volume remains lowered but after hanging up, if I open the App, the volume is raised and immediately lowered. As if the turnVolumeOn / turnVolumeOff calls remained on a waiting list.

I'm starting to think this app is above my ability. I really hope someone can help me understand. Thank you all for your attention
 
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you are saying it works on the emulator but doesn't work on your physical device?
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or it works on the emulator and when the phone is plugged in to the computer, but not when it's disconnected?
 
Emanuele Mazzante
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your interest. The code works well on the emulator, it works well on Marshmallow and Nougat physical devices in any situation (regardless if they are connected to the PC or if the activity is in bacground or in the foreground).

Instead when I test it on my personal device (Huawei / Honor 7x upgraded to Android Oreo) only works if connected to the PC. If disconnected, it stops working after a couple of minutes since the activity leaves the foreground.

I also created a small example on Github where I replaced the phone listener with a broadcast receiver registered inside the service but nothing has changed.

Thank for the help.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok it works on the emulator and on the physical device when it's plugged in.   If you disconnect it it stops working, right? If you don't build an app and then install it, the app won't work without being plugged in.  I'm guessing you ran the app on the phone and then unplugged it.  If you want to test the app unplugged you gotta make the apk  first then install it.  Go to build > build apk  or something like that.
 
Emanuele Mazzante
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for my english and thanks again for your help. However, if I disconnect the device while the app is running, nothing changes, the app works properly as long as it is in the foreground. If I press the home or back button (the activity leaves the foreground), the listener stops working correctly after one minute or two.

This behavior only happens on my personal device with Android Oreo. I'm aware of the limitations on broadcasts and background services in Android 8 but I used a foreground service and the receiver is registered at runtime inside that service.

I also used Gradle to install a debug release but nothing changed.

Honestly, I'm starting to believe it's a problem related to the manufacturer update. Unfortunately I don't have another physical device with Oreo.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the problem is only on one of your phones?  If so it must be a comparability issue.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compatibility
 
Emanuele Mazzante
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:So the problem is only on one of your phones?  If so it must be a comparability issue.



That's how it seems. But I have only a few physical devices to test the app and only one with Android Oreo so I'm not sure about it.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm it doesn't make sense that it doesn't work on Oreo as that is newer.  Have you tried debugging it on your computer?
 
Emanuele Mazzante
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:Hmm it doesn't make sense that it doesn't work on Oreo as that is newer.  Have you tried debugging it on your computer?



Of course, I tried to debug but if the device is connected to the PC the app works well and I do not notice anything strange.

I also tried to send the command in order to simulate the device not charging while connected via ADB in case there was some battery  optimization by the manufacturer, but everything works as it should when connected.

I have also added methods to log to external memory file in order to monitor method calls when the device is not connected. However, I only verified that the onCallStateChanged method (or the onReceive method if I use a broadcast receiver) is not triggered when it should.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you running the app? Did you build an app file and install it that way?
 
Emanuele Mazzante
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Hobbs wrote:How are you running the app? Did you build an app file and install it that way?



I used all the methods I know:

  • Debug App and Run App commands from Android Studio
  • install / installDebug from Gradle Tasks in Android Studio
  • build / assemble or assembleDebug from Gradle Tasks in Android Studio and after I installed the apk in the device

  •  
    Al Hobbs
    Bartender
    Posts: 667
    14
    TypeScript Fedora
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Did you find any exceptions in the debug after plugging it in after installing?
     
    Emanuele Mazzante
    Ranch Hand
    Posts: 77
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No exception is thrown. The foreground service is always active (the icon is displayed) but the lister or receiver stops to work after one minute or two since the activity leaves the foreground. That's all.
     
    Al Hobbs
    Bartender
    Posts: 667
    14
    TypeScript Fedora
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not really sure why it doesn't work.  It could be a setting on the phone or something that is preventing the function
     
    Emanuele Mazzante
    Ranch Hand
    Posts: 77
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also I think it's something specific about this particular device. I tried to disable any settings related to battery optimization but it doesn't seem to solve anything.

    Thanks anyway for the time you have dedicated to me
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic