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