Android Question DO NOT DISTURB mode

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Try this :
I got it from here http://stackoverflow.com/a/35772278


B4X:
    Dim ret As Int=-1
    Dim java As JavaObject
    java.InitializeContext
    Try
        ret =java.RunMethod("check_dnd",Null)
    Catch
        Log(LastException)
    End Try
   
    Select ret
       
    Case 0
        Log("DnD off")
    Case 2
        Log("Total Silence")
    Case 3
        Log("Alarms Only")
    Case -1
        Log("SettingNotFoundException occured ?")
    End Select

Java block
B4X:
#if java
    import android.provider.Settings.Global;
    import android.provider.Settings.SettingNotFoundException;
    public int check_dnd() throws SettingNotFoundException {
   
    return Global.getInt(getContentResolver(), "zen_mode");
   
   
    }


#end if
 
Upvote 1

hmotiwala

Member
Licensed User
Longtime User
Thanks a lot for your lightning fast response. I tried your code and it worked! However, the return code is always 0 or 1 depending on whether DnD is Off/On. This is good enough for me. My android version is 5.1.1, may be on latest version it returns other codes based on "alarm" and "Total Silence" options.
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
It did not work for my device :D (SettingNotFound)
just converted stackoverflow answer to work with B4A .
Glad it worked for you :)
 
Upvote 0

hmotiwala

Member
Licensed User
Longtime User
Try this :
I got it from here http://stackoverflow.com/a/35772278


B4X:
    Dim ret As Int=-1
    Dim java As JavaObject
    java.InitializeContext
    Try
        ret =java.RunMethod("check_dnd",Null)
    Catch
        Log(LastException)
    End Try
  
    Select ret
      
    Case 0
        Log("DnD off")
    Case 2
        Log("Total Silence")
    Case 3
        Log("Alarms Only")
    Case -1
        Log("SettingNotFoundException occured ?")
    End Select

Java block
B4X:
#if java
    import android.provider.Settings.Global;
    import android.provider.Settings.SettingNotFoundException;
    public int check_dnd() throws SettingNotFoundException {
  
    return Global.getInt(getContentResolver(), "zen_mode");
  
  
    }


#end if
I need to add this to a code or class module, seems it only works in a Service or Activity module.
Gives compile time error when adding to code or class module, any idea how to get it working?
Thanks
 
Upvote 0
Top