接收短信后如何将参数发送到另一个活动我尝试但显示错误
Intent类型中的方法
putExtra(String, boolean)
不适用于参数(Bundle)
下面是我的代码:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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 += msgs[i].getMessageBody().toString();
}
Intent l = new Intent(context,AgAppMenu.class);
Bundle bundle2 = new Bundle();
bundle.putString("msg", str);
l.putExtra(bundle);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
Toast.makeText(context, "SucessFull Login", Toast.LENGTH_SHORT).show();
答案 0 :(得分:0)
你可以将字符串直接放入意图:
l.putExtra("msg", str);
然后使用:
getIntent().getStringExtra("msg");
但如果您想使用捆绑包,我认为您应该参考 bundle2 :
Intent l = new Intent(context,AgAppMenu.class);
Bundle bundle2 = new Bundle();
bundle2.putString("msg", str);
l.putExtras(bundle2);
答案 1 :(得分:0)
使用共享首选项很容易。使用以下链接
How to use SharedPreferences in Android to store, fetch and edit values
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://android-er.blogspot.in/2011/01/example-of-using-sharedpreferencesedito.html
答案 2 :(得分:0)
您需要使用Intent.putExtras(Bundle)
答案 3 :(得分:0)
如果您想传递一个包,请使用putExtras (Bundle extras)
而不是putExtra