0

I am working on a project in which I have made app to default sms app and getting new message body and notification through Broadcast Receiver. It is showing toast that new message has been received and also read new message body.

But problems are

Problem 1:

The newly received sms is not retrieving from my default sms app inbox and not showing in my listview.

Problem 2:

I am not able to get each and every message from each conversation

GetMessage() code:

 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 code

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();
                }
            }
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Muhammad Ashfaq
  • 2,359
  • 5
  • 20
  • 46
  • When your app is the default, it is responsible for saving incoming messages to the Provider. Since you're not doing that, those messages aren't going to be in your query results. – Mike M. Jan 12 '19 at 14:54
  • Yes. i m making mistake. I haven't see this question which link you have given. – Muhammad Ashfaq Jan 12 '19 at 14:56
  • 1
    So thanks for helping and reference. – Muhammad Ashfaq Jan 12 '19 at 14:57
  • No problem. I would also mention that the default app should really be processing the `SMS_DELIVER` broadcast, instead of `SMS_RECEIVED`. – Mike M. Jan 12 '19 at 15:08
  • @MikeM. will you please tell me how can i delete complete inbox (defuaut) from my app which is not default ? is there any way ? – Muhammad Ashfaq Jan 15 '19 at 11:00
  • i am in huge issue. you have experience in sms for android . please guide me – Muhammad Ashfaq Jan 15 '19 at 11:00
  • 1
    "will you please tell me how can i delete complete inbox (defuaut) from my app which is not default ?" – You can't, really. There was a bug in 4.4 (KitKat) whereby a non-default app could get write access to the SMS Provider, but they fixed it in later versions. Since then, only the default app can delete messages on a standard system. – Mike M. Jan 15 '19 at 14:43
  • @MikeM. is there another way to delete or abort message before reaching to inbox without abortBroadcast() ....... bacause it is not working for me even i set my app(not default) to 999 or 1000. but message is also delived to inbox... – Muhammad Ashfaq Jan 15 '19 at 17:41
  • i am stuck from two days. can you please help me out ? – Muhammad Ashfaq Jan 15 '19 at 17:41
  • 1
    No, since 4.4 (KitKat), `abortBroadcast()` is ignored for the `SMS_RECEIVED` broadcast. Furthermore, the default app, which is what's writing those messages to the inbox, gets a different broadcast – `SMS_DELIVER` – which no other app gets, and which cannot be aborted or prevented anyway. On a standard system, since KitKat, there is no way to block or delete incoming text messages unless your app is the default. People have been trying to do that since it came about, and, apart from the bug in 4.4 I mentioned above, there is no way to do it. – Mike M. Jan 15 '19 at 17:58
  • @MikeM. when i make my app default and i am using this line context.getContentResolver().delete(Telephony.Sms.Inbox.CONTENT_URI, null,null) to delete system app inbox(which is not default now) completely but this is not giving me expected results? what will be the possible reasons ? – Muhammad Ashfaq Jan 15 '19 at 18:17
  • What results are you seeing, exactly? – Mike M. Jan 15 '19 at 18:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186730/discussion-between-muhammad-ashfaq-and-mike-m). – Muhammad Ashfaq Jan 15 '19 at 19:01

0 Answers0