1

我在将变量从 Activity 传递到 BroadcastReceiver 时遇到了一些问题……这是我的代码:

这是我的广播接收器代码...我尝试从我从活动中获得的一个电话号码中捕获短信...

public class SMSMonitor extends BroadcastReceiver 
{
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    public static String phone_number = "";
    public static String msg_body = "";
    public static final String SMS_EXTRA_NAME = "pdus";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String phone = intent.getExtras().getString("trusted_num");

        if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
        {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] messages = new SmsMessage[pduArray.length];

            for (int i = 0; i < pduArray.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
            }

            phone_number = messages[0].getDisplayOriginatingAddress();
            msg_body = messages[0].getMessageBody();

            System.out.println("Phone number: "+phone_number);
            System.out.println("Phone entered: "+phone);
        }
    }
}

这是我的活动代码:

public class Settings extends Activity implements OnClickListener{

    private Button btn_save;
    private EditText txt_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        //set Save button
        btn_save = (Button)findViewById(R.id.btn_save);
        txt_phone = (EditText)findViewById(R.id.et_phone);

        btn_save.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_settings, menu);
        return true;
    }

    @Override
    public void onClick(View v) 
    {
        if (v == btn_save)
        {
            try
            {
                String phone_num = txt_phone.getText().toString();
                Intent i = new Intent(Settings.this, SMSMonitor.class);
                i.putExtra("trusted_num", phone_num);
                sendBroadcast(i);
            }
            catch(Exception e)
            {
                System.out.println("Error: "+e.getLocalizedMessage());
            }
        }
    }

}

在这段代码中,我有用于输入电话号码的文本字段,我需要使用intent.putExtra()方法将其传递给 BroadcastReceiver,但在 LogCat 中我看到,该变量没有通过:

07-25 18:43:57.382: I/System.out(14245): Phone number: +37129690449
07-25 18:43:57.382: I/System.out(14245): Phone entered: null

那么我在这里做错了什么?

UPD 也许代码不正确,但它适用于我......

public void onReceive(Context context, Intent intent)
    {
        phone = intent.getExtras().getString("trusted_num");//get trusted phone number from Settings screen

        //receive SMS
        if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
        {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] messages = new SmsMessage[pduArray.length];

            for (int i = 0; i < pduArray.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
            }

            phone_number = messages[0].getDisplayOriginatingAddress();
            msg_body = messages[0].getMessageBody();

            System.out.println("Phone number: "+phone_number);
        }
        //check if number is not null
        if (phone != null && phone != "")
        {
            System.out.println("Phone entered: "+phone);
        }

    }
}
4

2 回答 2

1

您不能将意图传递给广播接收器。“BroadcastReceiver 无法查看或捕获与 startActivity() 一起使用的 Intent” https://developer.android.com/reference/android/content/BroadcastReceiver.html

不久前我遇到了类似的问题,并通过结合使用 IntentServices 和活动来解决它。你必须重组你的程序以适应这些指导方针

于 2013-07-25T15:57:41.553 回答
0

好吧,有些东西不匹配:

您首先发送的是没有任何操作的意图,但您指定的是 Broadcastreceiver 的类;不要那样做:

Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);

但请尝试:

Intent i = new Intent("my_package_name.Some_general_constant");
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);

然后,你BroadcastReceiver应该知道它也可以处理"Some_general_constant"动作。因此,请在您的清单文件中为您的SMSMonitor:

<receiver android:name=".package_to_bla_bla.SMSMonitor">
    <intent-filter>
        <action android:name="my_package_name.Some_general_constant"/>
    </intent-filter>
</receiver>

然后在您的 SMSMonitor 中,您需要添加一条else if语句来处理此广播:

else if("my_package_name.Some_general_constant".equals(intent.getAction()) 
    {
         // get the data from intent and use it 
    }
于 2013-07-25T16:22:21.727 回答