修改方法如下:
需要修改的文件路径如下
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这个广播,也可以达到类似的效果。。

更多精彩文章,请关注如下二维码对应公众号“代码视觉”。转载请联系授权!


Android 插拔充电器增加声音_第1张图片 代码视觉

更多相关文章

  1. GreenDao自动生成Android数据库操作代码
  2. Android源代码下载与编译
  3. 可分享到 Android、iOS 和 Flutter平台的Rust代码编译 [转]
  4. Android拍照上传功能示例代码
  5. Android DEX反编译后部分代码解析
  6. android: 大量开源项目源代码分享
  7. android重启代码
  8. android 发布正式版时用Gradle移除日志打印代码(添加混淆配置)

随机推荐

  1. android ---- ImageUtil工具类
  2. android开发【六】(基于android studio3.2
  3. Android CardView设置成普通的Framelayou
  4. Setting up ccache on android 4.0.3
  5. Eclipase插件下载地址
  6. Android放大镜的实现
  7. Android中如何判断内存卡是否存在
  8. android里面模拟按Home键的效果
  9. android使用属性动画执行抖动效果
  10. Internal error. Please refer to https: