0

My code has two activities.

public class OnCallingActivity extends Activity implement TextToSpeech.OnInitListener{}

public class TextReceiverActivity extends BroadcastReceiver{}

i want first activity to extend sencond activity

OnCallingActivity extends TextReceiverActivity {}

so I did this,

public class OnCallingActivity extends Activity implements TextToSpeech.OnInitListener {

  private TextReceiverActivity TextReceiverActivityClass;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.on_calling_layout);

    TextReceiverActivityClass= new TextReceiverActivity();
    speaker= new TextToSpeech(this, this);
    callerIdText=(TextView)findViewById(R.id.callerIdtextview);
    callerText=(TextView)findViewById(R.id.callermsgtextview);
    translatedText=(TextView)findViewById(R.id.translatedTextview);
    speakButton=(ImageButton)findViewById(R.id.btnSpeak);
    sendButton=(Button)findViewById(R.id.buttonsend);
    endCallButton=(ImageButton)findViewById(R.id.buttonendCall);        
}

my second activity is as follow,

public class TextReceiverActivity extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
     number ="";
     message="";

     final Bundle bundle = intent.getExtras(); 

     try {
        if(bundle != null) {
            final Object[] pduObjects = (Object[]) bundle.get("pdus");

            for(int i = 0; i < pduObjects.length; ++i) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pduObjects[i]);

                number = currentMessage.getDisplayOriginatingAddress();
                message = currentMessage.getDisplayMessageBody();

                Toast.makeText(context, "SENDER NUMBER: " + number + "; MESSAGE: " + message, Toast.LENGTH_LONG).show();
            }               
        }
     } catch (Exception e) {
        e.printStackTrace();
     }
  }

in first activity i called the method of second activity like this

public void onReceive(Context context, Intent intent) { 

    TextReceiverActivityClass.onReceive(context, intent);       

}

onReceive gives values for number and message..so how can i get them to my first activity to display them in TextViews.

onReceive is an inbuilt method so i can't change the return type.

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59

1 Answers1

0
  1. If TextReceiverActivity extends BroadcastReceiver, then TextReceiverActivity isnt an Activity and I would refrain from calling it an Activity.

  2. The onReceive method of a BroadcastReceiver is meant to be called from the Android sytem, not you.

  3. To send a message from an Activity to a BroadcastReceiver you would send a Broadcast. See the docs.

  4. If you want to send a message from your BroadcastReceiver to your Activity, there's a couple of questions about that.

Community
  • 1
  • 1
fweigl
  • 21,278
  • 20
  • 114
  • 205