我写了一个显示接收短信的程序。我将sms放入字符串变量时遇到了麻烦。
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Intent intentHome = new Intent(context,MainActivity.class);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);
如何将整个消息放入一个字符串变量中 我试过这个,,,
String S = new String(str) ;
它不起作用。
另外,我想将字符串setText用于ViewText变量
答案 0 :(得分:0)
我使用了你的代码,当我从MainActivity类调用消息时它运行良好。但是,实际上,我想在同一个类中使用它
public class SmsReceiver extends BroadcastReceiver
我写了
set_sms();
//---display the new SMS message---
Intent intentHome = new Intent(context,MainActivity.class);
intentHome.putExtra("msgContent", message);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);
}
void set_sms()
{
Activity x = new Activity() ;
Bundle extras = x.getIntent().getExtras();
if (extras != null) {
message = extras.getString("msgContent");
}
}
但是,总是有错误