Incoming Message Receiver and notifies the default system message app for Kitkat and above

Hello everyone I am developing a messaging app and follow all instructions from google blog here

and it works really well. But I have a problem where my app is running as the default messaging app. I want to store the message in my db app as well as the default device db app so that when the default app app error comes up all messages are also visible there.

The point is how I can notify my default messaging app to keep the message when I use my app as my default app.

Please, help! sorry for bad english

#Update 1 to save default db i use following code

public class SmsReceiver extends BroadcastReceiver {

private Context context;
private String msg_from;
//private MessageDataBaseAdapter messageDataBaseAdapter;
private String msgBody;
public static final String SMS_EXTRA_NAME = "pdus";
public static final String SMS_URI = "content://sms";

public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE = "date";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String BODY = "body";
public static final String SEEN = "seen";

public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;

public static final int MESSAGE_IS_NOT_READ = 0;
public static final int MESSAGE_IS_READ = 1;

public static final int MESSAGE_IS_NOT_SEEN = 0;
public static final int MESSAGE_IS_SEEN = 1;

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    if (intent.getAction().equals("android.provider.Telephony.SMS_DELIVER")) {
        HideSMSToast.showShortToast("Your Message Hasbeen received");

        Bundle bundle = intent.getExtras(); // ---get the SMS message passed
        // Get ContentResolver object for pushing encrypted SMS to incoming folder
        ContentResolver contentResolver = context.getContentResolver();                             // in---
        SmsMessage[] msgs = null;

        if (bundle != null) {
            // ---retrieve the SMS message received---
            try {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                // String strMessageFrom =
                // bundle.getDisplayOriginatingAddress();
                for (int i = 0; i < msgs.length; i++) {
                    //Calendar c = Calendar.getInstance();
                    // System.out.println("Current time => "+c.getTime());
                    // SCSLToast.showShortToast(c.getTime().toString());
                    //SimpleDateFormat date = new SimpleDateFormat(
                    //      "dd-MMM-yyyy hh:mm:ss a");
                    //String formattedDate = date.format(c.getTime());

                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    msg_from = msgs[i].getOriginatingAddress();
                    msgBody = msgs[i].getMessageBody();                     
                    //Db operation
                     saveSmsDataToDefaulDB( contentResolver, msgs[i] );
                }
                ///
                } catch (Exception e) {
                // Log.d("Exception caught",e.getMessage());
                //messageDataBaseAdapter.close();
            }
        }

    }
}

private void saveSmsDataToDefaulDB( ContentResolver contentResolver, SmsMessage sms )
 {
     // Create SMS row
     ContentValues values = new ContentValues();
     values.put( ADDRESS, sms.getOriginatingAddress());
     values.put( DATE, sms.getTimestampMillis());
     values.put( READ, MESSAGE_IS_NOT_READ);
     values.put( STATUS, sms.getStatus());
     values.put( TYPE, MESSAGE_TYPE_INBOX);
     values.put( SEEN, MESSAGE_IS_NOT_SEEN);        
     contentResolver.insert( Uri.parse(SMS_URI), values);
     HideSMSToast.showShortToast("Content written in default message app db");
 }}

      

and i got a message in defaul system messagenig app, but i got the address correctly and no other data. tell me how to store data in default db messages

Here are screenshots of how I got the message in the system standard sms app when I make my app as default app enter image description here

# Update 2 thanks guys for your support, I figured it was a stupid mistake I made. I am updating my code as follows

private void saveSmsDataToDefaulDB( ContentResolver contentResolver, SmsMessage sms )
 {
     // Create SMS row
     ContentValues values = new ContentValues();
     values.put( ADDRESS, sms.getOriginatingAddress());
     values.put( DATE, sms.getTimestampMillis());
     values.put( READ, MESSAGE_IS_NOT_READ);
     values.put( STATUS, sms.getStatus());
     values.put( TYPE, MESSAGE_TYPE_INBOX);
     values.put( SEEN, MESSAGE_IS_NOT_SEEN);
     values.put(BODY, sms.getMessageBody());
     contentResolver.insert( Uri.parse(SMS_URI), values);
    // HideSMSToast.showShortToast("Content written in default message app db");
 }

      

+3


source to share


1 answer


I think it should work, create a new class

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);


               // Show Alert
                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);

    }
}    

      

}



I mean, this code here prints in toast. But as soon as you receive a message and there is no sender, just submit it to your database.

hope this helps ...

+2


source







All Articles