简体   繁体   中英

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?

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM