Android手机上,当有其他录音软件正在录音,打开系统录音软件开始录音时会发生录音冲突,弹出对话框提示“无法开始新的录制,其他应用程序已在录制” ,如下图所示。(测试机为Samsung GT-I9508,android4.4.2)
这是由于android系统默认系统应用打开“显示通知”开关,且无法通过点击使其关闭。若想去除Android系统应用的通知功能,可利用如下代码:
private static void test(){ StringpName = "com.sec.android.app.voicerecorder"; int uid = 10157; boolean enable = false; Log.v("test","enter testjava"); INotificationManager nm =INotificationManager.Stub.asInterface(ServiceManager.getService("notification")); try { nm.setNotificationsEnabledForPackage(pName, uid, enable); } catch (Exception e){ e.printStackTrace(); Log.v("test", e.getMessage()); } }
注意:importandroid.app.INotificationManager;需要在工程中加入classes.jar(android 4.4版本),下载地址(http://download.csdn.net/detail/phoebe_2012/8700753)
普通应用执行上述代码会无法成功,因为该函数setNotificationsEnabledForPackage(Stringpkg, int uid, boolean enabled)中checkCallerIsSystem()函数校验uid检查调用程序有无权限。
boolean isUidSystem(int uid) { final int appid =UserHandle.getAppId(uid); return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid== 0); }
只有uid=1000或1001或0时才能调用该函数关闭通知功能。因而可通过起一个root权限的进程去执行jar包里的test()函数,具体代码见http://download.csdn.net/detail/phoebe_2012/8764383
在bin文件夹中生成test.jar后,需用dx.bat脚本将.class文件编译成class.dex文件,生成tt.jar,分别将可执行文件tt放入手机/system/bin文件夹下,tt.jar放入/system/framework文件夹下。以root权限执行tt后,去除了Android系统应用录音机的通知功能,使其在发生录音冲突时不再弹对话框提示。结果如下图所示。