今天在修改bug时,找到一文件packages\apps\Settings\src\com\android\settings\notification\NotificationSettings.java这个类经常修改,但之前都是有什么问题就修改一下问题,
并未对该文件做仔细阅读,今天一个bug原本只需要在类中现有的一广播中注册广播动作(AudioManager.RINGER_MODE_CHANGED_ACTION),然后在接收到该广播时做
相应的处理即可,但在系统发出广播后死活接收到不该 广播,就这问题弄了一个上午,最后还是一个同事帮忙解决,
文件中原有的广播注册:
final IntentFilter filter = new IntentFilter(); filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
filter.addDataScheme("file"); mContext.registerReceiver(this, filter);
final IntentFilter filter = new IntentFilter(); filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
filter.addDataScheme("file"); mContext.registerReceiver(this, filter);
一同事说要不你把:
filter.addDataScheme("file");
去掉看看,抱着试试看的心态一试,果然可以了,后面百度才知道加入与不加入filter.addDataScheme("file");有区别的,联系到广播的显式广播与隐式广播,广播的这两个方式区别这里就不做详细说明的,大家可以自己百度了解一下,
(2)ContentObserver,BroadcastReceiver,Handler各自用法,感觉下面这代码写的很不错,现记录下来,
声明:
private final H mHandler = new H(); private final SettingsObserver mSettingsObserver = new SettingsObserver(); private final Receiver mReceiver = new Receiver();
调用:
@Override public void onResume() { super.onResume(); refreshNotificationListeners(); lookupRingtoneNames(); mSettingsObserver.register(true); mReceiver.register(true); updateRingOrNotificationPreference(); updateEffectsSuppressor(); for (VolumeSeekBarPreference volumePref : mVolumePrefs) { volumePref.onActivityResume(); } } @Override public void onPause() { super.onPause(); mVolumeCallback.stopSample(); mSettingsObserver.register(false); mReceiver.register(false); }
private final class SettingsObserver extends ContentObserver { private final Uri VIBRATE_WHEN_RINGING_URI = Settings.System.getUriFor(Settings.System.VIBRATE_WHEN_RINGING); private final Uri NOTIFICATION_LIGHT_PULSE_URI = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); private final Uri LOCK_SCREEN_PRIVATE_URI = Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS); private final Uri LOCK_SCREEN_SHOW_URI = Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); public SettingsObserver() { super(mHandler); } public void register(boolean register) { final ContentResolver cr = getContentResolver(); if (register) { cr.registerContentObserver(VIBRATE_WHEN_RINGING_URI, false, this); cr.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, false, this); cr.registerContentObserver(LOCK_SCREEN_PRIVATE_URI, false, this); cr.registerContentObserver(LOCK_SCREEN_SHOW_URI, false, this); } else { cr.unregisterContentObserver(this); } } @Override public void onChange(boolean selfChange, Uri uri) { super.onChange(selfChange, uri); if (VIBRATE_WHEN_RINGING_URI.equals(uri)) { updateVibrateWhenRinging(); } if (NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { updatePulse(); } if (LOCK_SCREEN_PRIVATE_URI.equals(uri) || LOCK_SCREEN_SHOW_URI.equals(uri)) { updateLockscreenNotifications(); } } } private final class H extends Handler { private static final int UPDATE_PHONE_RINGTONE = 1; private static final int UPDATE_NOTIFICATION_RINGTONE = 2; private static final int STOP_SAMPLE = 3; private static final int UPDATE_EFFECTS_SUPPRESSOR = 4; private static final int UPDATE_RINGER_MODE = 5; private H() { super(Looper.getMainLooper()); } @Override public void handleMessage(Message msg) { switch (msg.what) { case UPDATE_PHONE_RINGTONE: mPhoneRingtonePreference.setSummary((CharSequence) msg.obj); break; case UPDATE_NOTIFICATION_RINGTONE: mNotificationRingtonePreference.setSummary((CharSequence) msg.obj); break; case STOP_SAMPLE: mVolumeCallback.stopSample(); break; case UPDATE_EFFECTS_SUPPRESSOR: updateEffectsSuppressor(); break; case UPDATE_RINGER_MODE: updateRingerMode(); break; } } } private class Receiver extends BroadcastReceiver { private boolean mRegistered; public void register(boolean register) { if (mRegistered == register) return; if (register) { final IntentFilter filter = new IntentFilter(); filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); mContext.registerReceiver(this, filter); } else { mContext.unregisterReceiver(this); } mRegistered = register; } @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) { mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR); } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE); } } }