簡體   English   中英

如何使用 NotificationListenerService android 獲取 Whatsapp 聯系號碼?

[英]How to fetch Whatsapp contact number using NotificationListenerService android?

我正在嘗試開發一個 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"));
            }
        }

    }
}

}

這對我有用:

//"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();    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM