1

the goal is to have a class called CallReceiver that implements BroadcastReceiver the class is listening for a call when there is a call it returns the caller number to the activity that created the instance.

example:

MainActivity:

CallReciver callreciver = new CallReciver(getApplicationContext());

CallReciver.java

public class CallReceiver extends BroadcastReceiver {

private Context mContext;

CallReceiver(Context context) {
    this.mContext = context;
}

@Override
public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        Intent i = new Intent(mContext, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("callMsg", incomingNumber);

        mContext.startActivity(i);
    }
}

I tried to send an Intent with the number when a call has been detected, but the problem is that the intent starts the activity and init the variables, I need just to get the number.

requered result: CallReceiver sends the number to MainActivity

Neriya Rosner
  • 43
  • 1
  • 10

1 Answers1

0

Editing answer because your requirement was not clear at first. if you want just the callers number in this receiver you might wanna try this snippet. Got this idea from this answer How to get phone number from an incoming call?

In your manifest:

<receiver android:name=".ServiceReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

In your reciever:

public class ServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                Intent i = new Intent(mContext, MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("callMsg", incomingNumber);
                mContext.startActivity(i);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }
Community
  • 1
  • 1
Nanites
  • 410
  • 3
  • 16
  • Wouldn't that require the activity to be on-screen? – Distjubo Mar 30 '16 at 12:18
  • No the local broadcast wont start activity it will only execute onRecieve of your activity if the activity is already running ! What exactly is your purpose do you want to start activity of your application as soon as the call receiver executes? – Nanites Mar 30 '16 at 12:21
  • @Nanites no, I want just to get the caller phone number and put it in a class variable – Neriya Rosner Mar 30 '16 at 12:41
  • @Nanites i what do i enter here? ( in the manifist its: `` ) `IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(` **action.PHONE_STATE** `); LocalBroadcastManager.getInstance(this).registerReceiver(receiver, intentFilter);` if i want to make this the main on recive without the class `CallReciever` – Neriya Rosner Mar 30 '16 at 12:42
  • See my edit, and also you may wanna be aware of the restrictions for such actions in newly coming android API – Nanites Mar 30 '16 at 12:51
  • @Nanites I can't extend BroadcastReciver in my main activity because I'm extending activity – Neriya Rosner Mar 30 '16 at 13:01
  • forget that one check the new answer, you need to get your broadcast registered in manifest and then apply a phonestate change listener in your receiver. – Nanites Mar 30 '16 at 13:03