Android 插拔充电器增加声音
修改方法如下:
需要修改的文件路径如下
frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java。
在这个类中的如下代码增加修改:
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //省略若干 ........ //通过增加对如下广播的监听,实现需要的效果。注意增加广播的注册 else if (action.equals(Intent.ACTION_POWER_CONNECTED) || action.equals(Intent.ACTION_POWER_DISCONNECTED)){ android.util.Log.d("fxjsound","charging sounds"); final boolean enabled = Settings.Global.getInt(getContext().getContentResolver(), Settings.Global.CHARGING_SOUNDS_ENABLED, 1) != 0; if (enabled) { final Uri soundUri = Uri.parse("file://" + "/system/media/ChargingStartOrEnd.ogg"); if (soundUri != null) { final Ringtone sfx = RingtoneManager.getRingtone(getContext(), soundUri); if (sfx != null) { sfx.setStreamType(AudioManager.STREAM_SYSTEM); sfx.play(); } } } } // end } };
广播的具体含义如下。
/** * Broadcast Action: External power has been connected to the device. * This is intended for applications that wish to register specifically to this notification. * Unlike ACTION_BATTERY_CHANGED, applications will be woken for this and so do not have to * stay active to receive this notification. This action can be used to implement actions * that wait until power is available to trigger. * * This is a protected intent that can only be sent * by the system. */ //直观意思就是充电器连上 // public static final String ACTION_POWER_CONNECTED = "android.intent.action.ACTION_POWER_CONNECTED"; /** * Broadcast Action: External power has been removed from the device. * This is intended for applications that wish to register specifically to this notification. * Unlike ACTION_BATTERY_CHANGED, applications will be woken for this and so do not have to * stay active to receive this notification. This action can be used to implement actions * that wait until power is available to trigger. * *
This is a protected intent that can only be sent * by the system. */ //直观意思就是充电器断开 // public static final String ACTION_POWER_DISCONNECTED = "android.intent.action.ACTION_POWER_DISCONNECTED";
除了上面这中广播之外,还可以监听android.hardware.usb.action.USB_STATE这个广播,也可以达到类似的效果。。
更多精彩文章,请关注如下二维码对应公众号“代码视觉”。转载请联系授权!
代码视觉
更多相关文章
- GreenDao自动生成Android数据库操作代码
- Android源代码下载与编译
- 可分享到 Android、iOS 和 Flutter平台的Rust代码编译 [转]
- Android拍照上传功能示例代码
- Android DEX反编译后部分代码解析
- android: 大量开源项目源代码分享
- android重启代码
- android 发布正式版时用Gradle移除日志打印代码(添加混淆配置)