传入消息Receiver并通知Kitkat及以上的默认系统消息应用程序

时间:2015-04-24 06:46:23

标签: android broadcastreceiver message

大家好我正在开发一个消息应用程序,并按照谷歌博客link here

的所有说明进行操作

它工作得非常好。但是当我的应用程序作为默认消息应用程序工作时我有一个问题我想在我的应用程序数据库中保存消息以及设备默认消息应用程序数据库,以便当设备消息应用程序变为默认时所有的消息都可以在那里看到。

当我使用我的应用程序作为默认应用程序时,点是我可以通知默认消息传递应用程序以保存消息。

请帮忙!抱歉英文不好

#Update 1 保存默认数据库我正在使用以下代码

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

我在defaul system messagenig app中收到了消息,但地址我得到了更正,没有其他数据.. 请告诉我如何在默认消息传递数据库中存储数据

以下是当我将我的应用作为默认应用时,我在系统默认短信应用中获得消息的屏幕截图 enter image description here

#Update 2 感谢大家的支持,我已经解决了,这是我做过的愚蠢错误。我正在更新我的代码,如下所示

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

1 个答案:

答案 0 :(得分:2)

我认为它应该有用,创建一个新类

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

    }
}    

}

我的意思是这里的代码是在toast中打印..但是一旦你收到消息和发送者没有,只需将它发送到你的数据库..

希望它有所帮助...