我正在BroadcastReceiver中创建一个新活动,并试图弄清楚如何在视图中显示动态文本。
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);
}
}
如何将incomingNumber传递给我刚开始的活动所显示的视图?
答案 0 :(得分:1)
与首先获得数字的方式相同,方法是为你的意图添加额外内容:
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);
(您可能希望更改此Intent的名称,因为您现在有两个名为intent
的Intent,这可能会在以后造成麻烦。)
并在onCreate()
public void onCreate(Bundle savedInstanceState) {
...
String string = getIntent().getStringExtra("IncomingNumber");
if(string != null) {
// Do something
}
}