【问题标题】:How to fetch Whatsapp contact number using NotificationListenerService android?如何使用 NotificationListenerService android 获取 Whatsapp 联系号码?
【发布时间】: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"));
            }
        }

    }
}

}

【问题讨论】:

    标签: java android whatsapp


    【解决方案1】:

    这对我有用:

    //"com.whatsapp"; // Personal    
    //"com.whatsapp.w4b"; // Business
    
    if (sbn.getPackageName().equals("com.whatsapp")){ 
        String title = sbn.getNotification().extras.getString(Notification.EXTRA_TITLE);
        String text = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();    
    }
    

    【讨论】:

    • 谢谢,但它只提供了联系人姓名。我必须运行 ContentResolver 查询来手动搜索合同
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多