Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android以编程方式阅读短信_Android_Sms_Broadcastreceiver - Fatal编程技术网

android以编程方式阅读短信

android以编程方式阅读短信,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,我试着用BroadcastReceiver阅读短信。我搜索并编写了以下代码: public class IncomingSms extends BroadcastReceiver { // Get the object of SmsManager final SmsManager sms = SmsManager.getDefault(); public void onReceive(Context context, Intent intent) { // Retrieves a

我试着用BroadcastReceiver阅读短信。我搜索并编写了以下代码:

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);

                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();
            } // end for loop
          } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }
}
在我的广播接收器中,我可以用吐司来显示所有收入信息,但我不需要它。我只需要具体的短信。例如,我需要阅读短信,它已经收到用户+4423232 我只需要阅读这个号码的用户短信


我怎样才能解决我的问题

只需通过编程方式获取用户的ph.No,或者向用户索要,然后使用if语句确定消息是否来自所需的数字,然后做任何你想做的事情 就像Jakar说的。如果您知道如何使用try语句,那么问题是什么?为什么不能使用if语句?

当然,您可以添加if senderNum.equals4423232,或者可以使用共享首选项或sqlite数据库,并检查数字是否应该以这种方式显示。我相信你知道如何使用if语句,那么你的问题到底是什么。。。或者你想从问这个问题中学到什么?