我有一个名为 SMSRecevier 的类,它接收和解释消息,并且必须根据消息执行某些任务。我有一个 RegisterPhoneActivity 和一个 RegisterPhoneFragment,我想在某个 SMS 到达时附加到该活动。我怎样才能做到这一点?我试过这个:在 SMSReceiver 中:
if (messageBody.contains(context.getResources().getString(R.string.numberResponse)))
{
String[] parts = messageBody.split(" ");
String number1 = parts[3];
String number2 = parts[5];
String number3 = parts[7];
RegisterPhoneActivity registerPhoneActivity=new RegisterPhoneActivity();
registerPhoneActivity.showNumberFragment(number1, number2, number3);
}
在注册电话活动中:
public void showNumberFragment(String no1, String no2, String no3)
{
View container=findViewById(R.id.fragment_container);
if (container!= null) {
RegisterPhoneFragment newFragment = new RegisterPhoneFragment();
Bundle args = new Bundle();
args.putString(RegisterPhoneFragment.NUMBER1, no1);
args.putString(RegisterPhoneFragment.NUMBER2, no2);
args.putString(RegisterPhoneFragment.NUMBER3, no3);
newFragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, newFragment).commit();
}