0

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

目标:

1.我想获取新消息发件人的姓名或号码(比如我的特定发件人不显示号码,这就像TM-Google , TM-MyGinger大多数电话推销发件人一样)

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

4

1 回答 1

2

创建短信接收器

    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

于 2012-06-06T05:14:12.687 回答