Android Question [SOLVED] - Setting "do not disturb" mode

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

Can B4A coding be used to set the "do not disturb" mode on the phone?

I found this coding on the forum to read what the current "do not disturb" mode is but I want to set that mode.

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
 

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for the link. It instructed me to put the permission in the manifest editor which I did and also tried to put it in my list of permissions but it's not available as a member. Could it be called something else?

Screenshot 2020-01-26 at 11.20.17.png

Despite not being able to include Permissions.Add(Starter.rp.PERMISSION_ACCESS_NOTIFICATION_POLICY) in my list of permissions for the app, I ran the app and the coding from your link and it called the screen that I needed to allow the app to access "Do not disturb". I was able to turn it on passing the number 2, but when I tried to pass it with number 0 to turn "Do not disturb" off, the app crashed. Looks like 0 is not a good number to pass. Also is there a way to not make the user not locate the app in the "Do not disturb" permission screen and allow the app to change "Do not disturb"?

B4X:
** Activity (main) Resume **
starter$ResumableSub_SetDoNotDisturbresume (java line: 311)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at b4a.example.starter$ResumableSub_SetDoNotDisturb.resume(starter.java:311)
    at b4a.example.starter._setdonotdisturb(starter.java:238)
    at b4a.example.starter._setdonotdisturboff(starter.java:337)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1083)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:1030)
    at b4a.example.main._donotdisturboff(main.java:556)
    at b4a.example.main._buttondonotdisturboff_click(main.java:535)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7333)
    at android.widget.TextView.performClick(TextView.java:14160)
    at android.view.View.performClickInternal(View.java:7299)
    at android.view.View.access$3200(View.java:846)
    at android.view.View.performClickInternal(View.java:7299)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27773)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6986)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalArgumentException: Invalid filter: 0
    at android.os.Parcel.createException(Parcel.java:1970)
    at android.os.Parcel.readException(Parcel.java:1934)
    at android.os.Parcel.readException(Parcel.java:1884)
    at android.app.INotificationManager$Stub$Proxy.setInterruptionFilter(INotificationManager.java:2686)
    at android.app.NotificationManager.setInterruptionFilter(NotificationManager.java:1673)
    ... 28 more
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.notification.NotificationManagerService$12.setInterruptionFilter(NotificationManagerService.java:3367)
    at android.app.INotificationManager$Stub.onTransact(INotificationManager.java:865)
    at android.os.Binder.execTransact(Binder.java:739)
At GPS_LocationChanged.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
All the information is included in that post.

No. It is not a "runtime permission". It is a special permission.

There is a link to the list of filters and their constant values.
This is the coding from the post I used to turn the "Do not disturb" on. I'm assuming the special permission you referred to is NOTIFICATION_POLICY_ACCESS_SETTINGS:
In the starter module:
Sub SetDoNotDisturb (Policy As Int)
    Dim NotificationManager As JavaObject
    NotificationManager = NotificationManager.InitializeContext.RunMethod("getSystemService", _
        Array("notification"))
    If NotificationManager.RunMethod("isNotificationPolicyAccessGranted", Null) = False Then
        Dim in As Intent
        in.Initialize("android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS", "")
        StartActivity(in)
        Wait For Activity_Resume '<---
        Log("after resume")
    End If
    If NotificationManager.RunMethod("isNotificationPolicyAccessGranted", Null) = True Then
        NotificationManager.RunMethod("setInterruptionFilter", Array(Policy))
    Else
        Log("No permission!")
    End If
End Sub

These are the permissions I'm using in the manifest editor:
In the manifest editor:
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.READ_CALL_LOG)
AddPermission(android.permission.CALL_PHONE)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_NOTIFICATION_POLICY)
AddPermission(android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS)

Is the NOTIFICATION_POLICY_ACCESS_SETTINGS line in the manifest editor correct? I still can't turn the "Do not disturb" off without the app crashing. Looks like 0 is not the correct number to pass.

All help is appreciated.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
No it is not. See the list of constants: https://developer.android.com/refer...cationManager.html#INTERRUPTION_FILTER_ALARMS


If it appears in that post, then yes it is correct.
I looked at the interruption filters and yes it looks like 0 is to turn it off. Since I don't know Java, the Java documentation in the link is very confusing.

I found in the post this line:
B4X:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

So I changed my manifest editor to look this this:
B4X:
AddPermission(android.permission.SEND_SMS)
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.READ_CALL_LOG)
AddPermission(android.permission.CALL_PHONE)
AddPermission(android.permission.ACCESS_FINE_LOCATION)

AddManifestText(
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />)
AddPermission(android.permission.ACCESS_NOTIFICATION_POLICY)

So I think I have all of the code correct based on the post but the app still crashes when 0 is passed to the sub routine. Please look at my log dump.

Please forgive me if I don't understand Java.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I found in the post this line:
I gave the solution in this specific post: https://www.b4x.com/android/forum/t...sturb-mode-programmatically.82794/post-524483
You don't need to add anything else. There is nothing related to Java here.

I looked at the interruption filters and yes it looks like 0 is to turn it off
Where have you seen it?

0 = FILTER_UNKNOWN: https://developer.android.com/refer...ationManager.html#INTERRUPTION_FILTER_UNKNOWN
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
@rleiman 1 = Off and 2 = On...
Hi Peter,

Thank you so much :D
It works perfect.

Is there a way to change the coding so the user is not required to navigate the "Do not disturb permission" screen the first time the app is run? If not, I will give the user instructions on working with that screen.

Thanks again.
 
Upvote 0
Top