Android Question java.lang.SecurityException: Not allowed to change Do Not Disturb state

MrKim

Well-Known Member
Licensed User
Longtime User
I am updating a V4 app to 26. I've been able to fix most of the issues. But not this one. One of the features of my app is the ability to silence the phone for a given period of time.
Error:
B4X:
java.lang.SecurityException: Not allowed to change Do Not Disturb state
    at android.os.Parcel.readException(Parcel.java:1693)
    at android.os.Parcel.readException(Parcel.java:1646)
    at android.media.IAudioService$Stub$Proxy.setRingerModeExternal(IAudioService.java:1264)
    at android.media.AudioManager.setRingerMode(AudioManager.java:1311)
    at anywheresoftware.b4a.phone.Phone.SetRingerMode(Phone.java:405)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$1.run(BA.java:325)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6688)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Failing line:
B4X:
            Ph.SetRingerMode(Ph.RINGER_SILENT)

Manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
    SetActivityAttribute(StopNagging2, android:theme, @android:style/Theme.Translucent.NoTitleBar)
    'SetActivityAttribute(StopNagging2, android:theme, @android:style/Theme)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddPermission(android.permission.RECEIVE_SMS)
AddPermission(android.permission.PROCESS_OUTGOING_CALLS)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)  '6/4/18 - required for 26
AddPermission(android.permission.ACCESS_NOTIFICATION_POLICY)  '6/4/18 - required for 26
AddReceiverText(DoSMSNotification, <intent-filter>    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
AddPermission(android.permission.READ_PHONE_STATE)
AddReceiverText(DoNotification, <intent-filter>    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>)
AddReceiverText(DoNotification, <intent-filter>    <action android:name="android.intent.action.SERVICE_STATE" />
</intent-filter>)
AddReceiverText(DoNotification, <intent-filter>    <action android:name="android.provider.Telephony.LISTEN_MESSAGE_WAITING_INDICATOR" />
</intent-filter>)
AddReceiverText(UserPresent, <intent-filter>    <action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>)
AddReceiverText(UserPresent, <intent-filter>    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>)
'End of default text.

AAAND - I am running these lines (and Allowing):

B4X:
RP.CheckAndRequest(RP.PERMISSION_READ_PHONE_STATE)
RP.CheckAndRequest(RP.PERMISSION_READ_EXTERNAL_STORAGE)
RP.CheckAndRequest(RP.PERMISSION_PROCESS_OUTGOING_CALLS)
RP.CheckAndRequest(RP.PERMISSION_RECEIVE_SMS)
I could find a Check and Request for ACCESS_NOTIFICATION_POLICY
Thanks for your help
 
Last edited:

MrKim

Well-Known Member
Licensed User
Longtime User

Yes, I saw that, but I was a little unclear. That link is setting Volume, I am setting SetRingerMode.
So does that code apply to ALL of the Phone Settings or just some? I am using
SetRingerMode, GtetRingerMode, SetMute, SetVolume, GetMaxVolume, and GetVolume
Does the Code inside the If p.SdkVersion >= 23 need to be run every time you use a Phone function or only once when the app starts?
Should it be in a service Module or can it be in an activity?
Any thoughts of building that code into the Phone Lib:D?
 
Last edited:
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Hi
a small amendment
If p.SdkVersion >= 24 Then
it seems that setting 23 causes errors on android 6.0
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
HI

(LG G3 Android 6.0)

B4X:
** Activity (main) Pause, UserClosed = false **
getSlotFromBufferLocked: unknown buffer: 0xa5c9ebc0
Failed to destroy process 2165
android.system.ErrnoException: kill failed: ESRCH (No such process)
    at libcore.io.Posix.kill(Native Method)
    at libcore.io.ForwardingOs.kill(ForwardingOs.java:105)
    at java.lang.ProcessManager$ProcessImpl.destroy(ProcessManager.java:260)
    at anywheresoftware.b4a.remotelogger.RemoteLogger.LogCatStop(RemoteLogger.java:132)
    at anywheresoftware.b4a.remotelogger.RemoteLogger.handleIncomingData(RemoteLogger.java:62)
    at anywheresoftware.b4a.remotelogger.Connector.readData(Connector.java:134)
    at anywheresoftware.b4a.remotelogger.Connector.mainLoop(Connector.java:84)
    at anywheresoftware.b4a.remotelogger.Connector.run(Connector.java:53)
    at java.lang.Thread.run(Thread.java:818)
-1 received
writer error
java.lang.InterruptedException
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:1988)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2022)
    at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:357)
    at anywheresoftware.b4a.remotelogger.Connector$Writer.run(Connector.java:160)
    at java.lang.Thread.run(Thread.java:818)
Starting remote logger. Port: 29233
After accept

It seems to me that in the android 6.0 version this permission is not required.

This error appeared in the developer's console:
B4X:
anywheresoftware.b4a.BA.raiseEvent2 (BA.java:197)
anywheresoftware.b4a.keywords.Common$11$1.run (Common.java:1139)
anywheresoftware.b4a.BA.setActivityPaused (BA.java:437)
fixxxxx.xxxxx.main$ResumeMessage.run (main.java:306)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:148)
android.app.ActivityThread.main (ActivityThread.java:5551)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)
 
Last edited:
Upvote 0
Top