无法从我自己的默认SMS应用收件箱中检索新到邮件

时间:2019-01-12 09:37:37

标签: android sms android-contentprovider android-broadcastreceiver

我正在一个项目中,我已经将 app设置为默认的短信应用,并通过Broadcast Receiver获得了新的消息正文和通知。它向吐司表明已经接收到新消息,并且还读取了新消息正文。

但是问题是

问题1:

新收到的短信未从我的默认短信应用收件箱中检索,也未显示在列表视图中。

问题2:

我无法从每次对话中获得每条消息

GetMessage()代码:

 public ArrayList<String> getSms() {
        ContentResolver contentResolver=getContentResolver();
        Uri mSmsQueryUri = Uri.parse("content://sms/inbox");
        ArrayList<String> messages = new ArrayList<String>();
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(mSmsQueryUri, new String[] { "_id", "address", "date", "body",
                    "type", "read" }, null, null, "date desc");
            if (cursor == null) {
                Log.i("curson null", "cursor is null. uri: " + mSmsQueryUri);
                Toast.makeText(this, "curor null", Toast.LENGTH_SHORT).show();
            }

            for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) {

                String body = cursor.getString(cursor.getColumnIndex("body"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
               /* String date= cursor.getString(cursor.getColumnIndex("date"));
                SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY-MM-DD",Locale.US);
                Date datee= dateFormat.parse(date);*/

                messages.add("\n"+ "Sender:     "+ address+"\n"+"Content:      "+body+"\n");
                //messages.add(address);
            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
        } finally {
            cursor.close();
        }

        return messages;
    }

MyListner代码

public void onReceive(Context context, Intent intent) {

        //Getting new Message notification
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle=intent.getExtras();



            if(bundle!=null){
                Toast.makeText(context, "New Message Received", Toast.LENGTH_SHORT).show();
                try {

                    Object[] pdus = (Object[]) bundle.get("pdus");
                    SmsMessage[] msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++) {
                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        message_from = msgs[i].getOriginatingAddress();
                         msgBody = msgs[i].getMessageBody();
                    }
                        Toast.makeText(context, "Message sent from:  " + message_from, Toast.LENGTH_SHORT).show();
                        if(msgBody.equals("Where are you")){
                            sendMessage(context);
                            Toast.makeText(context, "Message Body:       " + msgBody, Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(context, "Other message", Toast.LENGTH_SHORT).show();
                        }


                }catch (Exception e){
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
    }

0 个答案:

没有答案