如何从broadcastReceiver接收短信后更新收件箱?安卓

时间:2016-02-16 08:47:51

标签: android

我正在创建一个短信应用,我被卡在短信接收器中, 这是我的短信收件人代码

public class SmsReceiver extends BroadcastReceiver {

public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {
        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
        String smsMessageStr = "";
        for (int i = 0; i < sms.length; ++i) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

            String smsBody = smsMessage.getMessageBody().toString();
            String address = smsMessage.getOriginatingAddress();

            smsMessageStr += "SMS From: " + address + "\n";
            smsMessageStr += smsBody + "\n";
        }
        Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
        }
    }
}

它可以很好地制作好的吐司,但它不会更新收件箱,如何更新收件箱? (context:// inbox)

2 个答案:

答案 0 :(得分:0)

在收件箱活动中添加接收器

class UpdateInboxBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        updateInbox(); // custom implementation for updating inbox adapter
    }

}

然后在您的接收器中发送广播到您的收件箱活动

public void sendBroadcast(String message){

    Intent broadCastIntent = new Intent();
    broadCastIntent.putExtra("message", message);
    broadCastIntent.setAction("updateInbox");
    this.sendBroadcast(broadCastIntent);
    stopSelf();
}

public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
    Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
    String smsMessageStr = "";
    for (int i = 0; i < sms.length; ++i) {
        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])    sms[i]);

        String smsBody = smsMessage.getMessageBody().toString();
        String address = smsMessage.getOriginatingAddress();

        smsMessageStr += "SMS From: " + address + "\n";
        smsMessageStr += smsBody + "\n";
    }
    Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
sendBroadcast(smsMessageStr);
    }
}
}

答案 1 :(得分:0)

最后我解决了它,这是它的完成方式,希望它可以帮助某人

添加contentValues并通过context.getContentResolver传递它

public void onReceive(Context context, Intent intent) {
    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {
        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
        ContentResolver contentResolver = context.getContentResolver();
        String smsMessageStr = "";
        for (int i = 0; i < sms.length; ++i) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

            String smsBody = smsMessage.getMessageBody().toString();
            String address = smsMessage.getOriginatingAddress();

            smsMessageStr += "SMS From: " + address + "\n";
            smsMessageStr += smsBody + "\n";

            /*creating the content values to pass to inbox*/
            ContentValues values = new ContentValues();
            values.put("address",address);
            values.put("body",smsBody);

            Uri uriSMSURI = Uri.parse("content://sms/inbox");
           //contentResolver.insert(uriSMSURI, values);

            /*push to inbo*/
            context.getContentResolver().insert(uriSMSURI,values);

        }
        Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
    }
}