如何在接收短信时将参数发送到另一个活动

时间:2012-09-11 08:26:06

标签: android

接收短信后如何将参数发送到另一个活动我尝试但显示错误

  

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();

4 个答案:

答案 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)

答案 2 :(得分:0)

您需要使用Intent.putExtras(Bundle)

设置捆绑包

答案 3 :(得分:0)

如果您想传递一个包,请使用putExtras (Bundle extras)而不是putExtra