【问题标题】:Reading message content and Message sender?阅读消息内容和消息发送者?
【发布时间】:2012-06-06 04:57:46
【问题描述】:

我正在构建一个应用程序,它将从收件箱中读取每条新短信 它来自特定发件人,我的应用会读取内容,如果它有一些特定内容,那么它会执行一些操作。

目标:

1. 我想获取新消息发件人的姓名或号码(假设我的特定发件人不显示号码,它类似于TM-Google , TM-MyGinger 主要是电话营销发件人

2. 如果它来自我正在搜索的人,那么我想阅读消息的内容。 其他是我的部分。 请提供一些想法或代码sn-p。

【问题讨论】:

  • 你尝试过收听每条短信,你需要使用 ContentObserver 来观看收件箱

标签: android sms message


【解决方案1】:

创建短信接收器

    public class SMSReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

        if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdusObj.length];

                // getting SMS information from Pdu.
                for (int i = 0; i < pdusObj.length; i++) {
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                }

                for (SmsMessage currentMessage : messages) {
                    // currentMessage.getDisplayOriginatingAddress()   has sender's phone number
                    // currentMessage.getDisplayMessageBody()     has the actual message
            }
        }
    }
}

您可以使用以下代码阅读收件箱:

Uri mSmsinboxQueryUri = Uri.parse("content://sms");
            Cursor cursor1 = getContentResolver().query(
                    mSmsinboxQueryUri,
                    new String[] { "_id", "thread_id", "address", "person", "date",
                            "body", "type" }, null, null, null);
             startManagingCursor(cursor1);
            String[] columns = new String[] { "address", "person", "date", "body",
                    "type" };
            if (cursor1.getCount() > 0) {
                String count = Integer.toString(cursor1.getCount());
                Log.e("Count",count);
                while (cursor1.moveToNext()) {
                    String address = cursor1.getString(cursor1
                            .getColumnIndex(columns[0]));
                    String name = cursor1.getString(cursor1
                            .getColumnIndex(columns[1]));
                    String date = cursor1.getString(cursor1
                            .getColumnIndex(columns[2]));
                    String msg = cursor1.getString(cursor1
                            .getColumnIndex(columns[3]));
                    String type = cursor1.getString(cursor1
                            .getColumnIndex(columns[4]));
                    et.setText( et.getText() + "Address:" + address + "\n"
                            + "Name:" + name + "\n"
                            + "Date:" + date + "\n"
                            + "MSG:" + msg + "\n"
                            + "type:" + type + "\n"
                            );



               }
           }

向清单添加流

<receiver android:name="SMSReceiver" android:enabled="true">
    <intent-filter>
       <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
  </receiver>

并为READ_SMSRECEIVE_SMS 添加权限

【讨论】:

  • 天气它将能够读取非数字形式的发件人地址
  • 是的。如果发件人是TM-MyGinger,地址返回“TM-MyGinger”
  • 我太忙了,请多多包涵,我会尽快完成的
  • 我已经开始了这个项目,但它正在等待中,我会尽快完成,谢谢
  • 从收件箱中读取短信需要 android.permission.READ_SMS
猜你喜欢
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2021-01-06
  • 2020-01-25
相关资源
最近更新 更多