0

I am using the Broadcast receiver to detect the outgoing calls and the following code is working fine in an independent code.

My question is, how can i call this method in my main activity, since i want to compare the string number and do an action then. Please help me! Thanks.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class OutgoingCallReceiver extends BroadcastReceiver  {
    @Override
    public void onReceive(Context context, Intent intent) {
        String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.d("DEBUG", "RINGING (outgoing call) with number: " + number);
        Toast.makeText(context, "Outgoing call number: " + number, Toast.LENGTH_LONG).show();    
    }
}
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
Kim
  • 23
  • 8
  • You can use this class inside your MainActivity class. – Siddharth_Vyas Dec 09 '13 at 06:32
  • what do you mean by "how can i call this method in my main activity"? Its a broadcast receiver. onReceive() will only execute when you call someone.. Please, be precise with your quest. – Abhishek Shukla Dec 09 '13 at 06:33
  • @AbhishekShukla, I read that you can't call a class, you only call a method, However I want to compare the value number in my main activity and if the value match i will process an action! I know i am a beginner, but i need this for my uni project. – Kim Dec 09 '13 at 06:40
  • If the action does not require any user interaction, I'd suggest you to do the action in onReceive() itself. Provided that the number you are comparing with, is accessible. :) – Abhishek Shukla Dec 09 '13 at 06:43
  • @AbhishekShukla, the action is to send an email which is on the mainActivity, this is why even if i put it in this class, i will have to call the mainActivity!! I simply want to add something in the mainActivity which will compare the (number) from this class to a specific number and if it is true, it will send an email T_T – Kim Dec 11 '13 at 18:10

1 Answers1

0

You can pass the entire Intent to your activity by passing it as an extra flag. find below your edited code that pass the intent to the new activty created:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class OutgoingCallReceiver extends BroadcastReceiver  {
    @Override
    public void onReceive(Context context, Intent intent) {
        String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.d("DEBUG", "RINGING (outgoing call) with number: " + number);
        Toast.makeText(context, "Outgoing call number: " + number, Toast.LENGTH_LONG).show(); 

        // You can go to another activity by the below code
        Intent i = new Intent(context, OutgoingCallDisplay.class);
        i.putExtras(intent);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i) 
    }
}

If you have any question related to my answer, do not hesitate to contact me. Good luck

Nabz
  • 390
  • 2
  • 14
  • Thank you for your answer ^^ , but do you mean i update my current OutgoingReceiver class as you did and it will pass the intent to my mainacivity? How can i reuse my string (number) in my mainactivity?@AndroidNabz – Kim Dec 11 '13 at 14:44
  • FYI, you can pass variables in the intent. So you can put the following : i.putExtra(OutgoingCallDisplay.NUMBER_VAR, number); and in your activity you can retrevie it using : String nymberRetreived = getIntent().getStringExtra(OutgoingCallDisplay.NUMBER_VAR); – Nabz Dec 12 '13 at 10:41