0

I have used the following code to get the phone number. I stored the number in a static variable. When I tried to use that variable in another activity, it throws NullPointer Exception.

package com.income;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class IncomingCallReceiver extends BroadcastReceiver {
    static String info = null;
    static String phonenumber;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Bundle bundle = intent.getExtras();
        if (null == bundle)
            return;
        Log.i("IncomingCallReceiver", bundle.toString());
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        Log.i("IncomingCallReceiver", "State: " + state);
        if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
            phonenumber = bundle
                    .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            // String d=bundle.getString(TelephonyManager.EXTRA_STATE_IDLE)
            Log.i("IncomingCallReceiver", "Incomng Number: " + phonenumber);
            info = phonenumber;
        }
        if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
            Toast.makeText(context, info, Toast.LENGTH_LONG).show();
            Intent intent1 = new Intent(context, MissedCall.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent1);
        }
    }

}
Manikandan
  • 1,479
  • 6
  • 48
  • 89

2 Answers2

0

Based on the fact that you are getting a NullPointerException this means that the bundle is null which makes the pone number be null.

I suggest you take a look on how to get the caller's phone number (here) like how it's done in this post.

EDIT

Well what I see is that you are never storing the phone number in the intent that starts the activity, so you will never get the phone number on said activity. Try using intent1.putExtra("phoneNumber", phoneNumber); now that the phone number in BroadcastReciever is not null, and retrieve it form MissedCall.class using intent.getExtras("phoneNumber"); where you are trying to get the number.

Community
  • 1
  • 1
Alvin Baena
  • 903
  • 1
  • 10
  • 24
0

check your intent.putstring() method

DineshKumar
  • 1,641
  • 15
  • 13