Hi I am a novice Java and Android programmer and apologies if this is a bit of a dumb question.
I have seen many examples recently where the top voted solution includes code that will not resolve/run when i cut and paste it into my code.
My main issue is that I cannot work out how to change 'context.' given in the example below to something that will run.
As an example
I get this error ...
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dummies.silentmodetoggle, PID: 12988
java.lang.SecurityException: Not allowed to change Do Not Disturb state
at android.os.Parcel.createExceptionOrNull(Parcel.java:2285)
at android.os.Parcel.createException(Parcel.java:2269)
at android.os.Parcel.readException(Parcel.java:2252)
at android.os.Parcel.readException(Parcel.java:2194)
at android.media.IAudioService$Stub$Proxy.setRingerModeExternal(IAudioService.java:2835)
at android.media.AudioManager.setRingerMode(AudioManager.java:1150)
at com.dummies.silentmodetoggle.util.RingerHelper.performToggle(RingerHelper.java:20)
at com.dummies.silentmodetoggle.MainActivity$1.onClick(MainActivity.java:41)
at android.view.View.performClick(View.java:7350)
at android.view.View.performClickInternal(View.java:7327)
at android.view.View.access$3600(View.java:807)
at android.view.View$PerformClick.run(View.java:28166)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7464)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.audio.AudioService.setRingerModeExternal(AudioService.java:3169)
at android.media.IAudioService$Stub.onTransact(IAudioService.java:1257)
at android.os.Binder.execTransactInternal(Binder.java:1138)
at android.os.Binder.execTransact(Binder.java:1102)
I/Process: Sending signal. PID: 12988 SIG: 9
The Solution is in:
https://stackoverflow.com/questions/39151453/in-android-7-api-level-24-my-app-is-not-allowed-to-mute-phone-set-ringer-mode#
When I cut and paste this code in:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
many of the parts do not initially work but after importing a few classes most of the issues go away apart from:
context.getSystemService(Context.NOTIFICATION_SERVICE); where it can't work out what to use for context
and
startActivity(intent); where it cannot find the class or what to use for intent.
As an aside, I am not looking for an answer to the Notification issue, I am after help with understanding how do I successfully take Stack overflow code paste it into my code and work out where the missing parts come from, particularly 'context.'.
I am working through a few different Android learning books including Android App Development for Dummies which is where this example is from. I have got the code to work by using an emulator with Lollipop code as this predates the introduction of the newish DND functionality.
Finally, I am trying to get it straight in my head how to use the reference guide as I come from a mainframe background where they seem to use a different way of explaining things.
All help gratefully received
Cheers Craig