Reputation: 33
I am creating an application and a certain part of my code I need to create a notification when I receive a call. The problem is that I am not able to implement the notification, since I have to pass the number of who is calling me. The following pieces of my code.
My Receiver:
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
CallIntercepterListener callIntercepterListener = new CallIntercepterListener();
telephonyManager.listen(callIntercepterListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.d(TAG, "phoneNumber: " + phoneNumber);
}
and My Listener:
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.v(TAG, "Event Call: " + incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// Create a notification with de incomingNumber
break;
}
}
Upvotes: 0
Views: 202
Reputation: 10977
What do yo need the listener for? Your receiver gets already called whenever the phone state changes.
here is a nice article about how to use the BroadcastReceiver.
From this article:
package de.vogella.android.receiver.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("DEBUG", phoneNumber);
}
}
}
}
Upvotes: 1