1. 目的
广播拦截中,注意识别默认电话进程和外拨事件不可拦截
2. 源码
2.1 拦截判断
// check out call for phone app
if ("android".equals(callerApp)
&& intent != null && Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())
&& mSystemPreferredConfig.isDefaultPhoneApp(targetApp)) {
if (DEBUG) Slog.d(TAG, "start phone app for out call:" + targetApp);
return true;
}
2.2 是否为默认电话进程
private String mDefaultPhoneApp;
public boolean isDefaultPhoneApp(String pkgName) {
return (pkgName != null
&& mDefaultPhoneApp != null
&& pkgName.equals(mDefaultPhoneApp));
}
private SettingsObserver mSettingsObserver;
class SettingsObserver extends ContentObserver {
private final ContentResolver mResolver;
final Uri defaultSMSUri = Settings.Secure.getUriFor(
// 默认短信
Settings.Secure.SMS_DEFAULT_APPLICATION);
final Uri defaultPhoneUri = Settings.Secure.getUriFor(
// 默认电话
Settings.Secure.DIALER_DEFAULT_APPLICATION);
SettingsObserver(Handler handler, ContentResolver resolver) {
super(handler);
mResolver = resolver;
// mResolver.registerContentObserver(Settings.Secure.getUriFor(
// Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES), false, this);
if (defaultSMSUri != null)
mResolver.registerContentObserver(defaultSMSUri, false, this, UserHandle.USER_ALL);
if (defaultPhoneUri != null)
mResolver.registerContentObserver(defaultPhoneUri, false, this, UserHandle.USER_ALL);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
// getEnabledServiceList();
try {
if (defaultSMSUri !=null && defaultSMSUri.equals(uri)) {
mDefaultSmsComp = SmsApplication.getDefaultSmsApplication(mContext, true);
if (DEBUG) Slog.d(TAG, "mDefaultSmsComp:" + mDefaultSmsComp + " mDefaultPhoneApp:" + mDefaultPhoneApp);
} else if (defaultPhoneUri != null && defaultPhoneUri.equals(uri)) {
mDefaultPhoneApp = DefaultDialerManager.getDefaultDialerApplication(mContext);
if (DEBUG) Slog.d(TAG, "mDefaultSmsComp:" + mDefaultSmsComp + " mDefaultPhoneApp:" + mDefaultPhoneApp);
}
} catch (Exception e) {}
}
}