Created
December 31, 2021 19:16
-
-
Save Low-power/555e38fc2a9fab7d21f953994cd8cafb to your computer and use it in GitHub Desktop.
Prevent Android-based phone enters silent mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2015-2022 Rivoreo | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE | |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package rivoreo.android.nosilent; | |
import android.media.AudioManager; | |
import android.media.IAudioService; | |
import android.content.Context; | |
import android.os.IBinder; | |
import android.os.ServiceManager; | |
//import java.lang.reflect.Method; | |
//import java.lang.reflect.InvocationTargetException; | |
class Program { | |
private static void check_ring_settings_loop(IAudioService audiosrv) { | |
while(true) { | |
int ring_volume = audiosrv.getStreamVolume(AudioManager.STREAM_RING); | |
if(ring_volume == 0) { | |
// Must set to vibrate mode first to leave silent mode | |
audiosrv.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); | |
} | |
if(ring_volume < 5) { | |
audiosrv.setStreamVolume(AudioManager.STREAM_RING, 5, AudioManager.FLAG_ALLOW_RINGER_MODES); | |
} | |
try { | |
Thread.sleep(10000); | |
} catch(InterruptedException e) { | |
} | |
} | |
} | |
//public static void main(String[] a) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { | |
public static void main(String[] a) { | |
//AudioManager audiomgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); | |
IBinder binder = ServiceManager.getService(Context.AUDIO_SERVICE); | |
/* | |
Class<?> servicemanager_class = Class.forName("android.os.ServiceManager"); | |
Method get_service_method = servicemanager_class.getDeclaredMethod("getService", String.class); | |
IBinder binder = (IBinder)get_service_method.invoke(null, Context.AUDIO_SERVICE); | |
*/ | |
IAudioService audiosrv = IAudioService.Stub.asInterface(binder); | |
check_ring_settings_loop(audiosrv); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment