【发布时间】:2020-09-07 12:26:37
【问题描述】:
我正在尝试开发一个 android 应用程序来检测传入的电话和 Whatsapp 呼叫并获取呼叫者联系人/电话号码。我能够使用 BroadcastReceiver 检测电话呼叫。
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("Caller", "Called");
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("Caller", phoneNumber);
}
}
}
}
问题是我无法使用此代码获取 Whatsapp 调用。 我试过 NotificationListenerService。每当有来电时,就会在 Whatsapp 上发布通知。我可以检测来电,但我可以从发布的通知中获取联系方式。 这是我的 NotificationListenerService 代码
public class NotificationReceiver extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(sbn.getPackageName().equals("com.gbwhatsapp"))
{
Log.e("Called", "Sbn Key : "+ sbn.getKey());
Bundle bundle = sbn.getNotification().extras;
if (bundle != null) {
for (String key : bundle.keySet()) {
Log.e("Called", key + " : " + (bundle.get(key) != null ? bundle.get(key) : "NULL"));
}
}
}
}
}
【问题讨论】: