如何将数据从BroadcastReceiver传输到活动? 我故意尝试了它,但它返回null。
MyBroadcastReceiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
......................................
......................................
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent i = new Intent(context, CalendarCall.class);
i.putExtra("number", phoneNumber);
(startActivity(i)-> i can't start Activity under BroadcastReceiver,what is another way to do that? )
Log.d(TAG, phoneNumber);
}
}
}
}
主要活动:
Intent intent = getIntent();
String number = intent.getStringExtra("number");
Log.d(TAG, "Number: " + number);
答案 0 :(得分:1)
MyBroadcastReceiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
......................................
......................................
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent i = new Intent(context, CalendarCall.class);
i.putExtra("number", phoneNumber);
context.startActivity(i);
Log.d(TAG, phoneNumber);
}
}
}
}
试用此代码