Ben McCann
Ben McCann

Reputation: 18994

Displaying dynamic text in Android

I'm creating a new activity in a BroadcastReceiver and am trying to figure out how to display dynamic text in the view.

public class CallReceiver extends BroadcastReceiver {

  @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);

      SystemClock.sleep(1);
      Intent myIntent = new Intent(context, CallerIdActivity.class);
      myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
      context.startActivity(myIntent);
  }
}

How can I pass incomingNumber to the view displayed by the activity I just started?

Upvotes: 0

Views: 89

Answers (1)

Sam
Sam

Reputation: 86948

The same way that you got the number in the first place, by adding an extra to your Intent:

  Intent intent = new Intent(context, CallerIdActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
  intent.putExtra("IncomingNumber", incomingNumber);
  context.startActivity(intent);

(You may want to change this Intent's name because you now have two Intents named intent which may cause trouble later.)

And check for this extra in onCreate()

public void onCreate(Bundle savedInstanceState) {
    ...
    String string = getIntent().getStringExtra("IncomingNumber");
    if(string != null) {
        // Do something
    }
}

Upvotes: 1

Related Questions