在默认应用程序中接收SMS

时间:2015-04-17 18:55:25

标签: android sms broadcastreceiver android-5.0-lollipop android-4.4-kitkat

我的应用是默认应用。 Android版:Lollipop。

它的代码是:

清单:

 <!-- BroadcastReceiver that listens for incoming SMS messages -->
    <receiver
        android:name=".SmsReceiver"
        android:permission="android.permission.BROADCAST_SMS" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
        </intent-filter>
    </receiver>

代码:

@Override
public void onReceive(Context arg0, Intent intent) {
    final Bundle bundle = intent.getExtras();
    this.context = arg0;
    try {
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus.length == 0) {
                return;
            }
            ContentResolver contentResolver = arg0.getContentResolver();
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                putSmsToDatabase(contentResolver, messages[i]);
            }
        } else {
            Log.e("", "bundle is null");
        }
    } catch (Exception e) {
        Log.e("SMS", "Exception: " + e);
    }
}
private void putSmsToDatabase(ContentResolver contentResolver,
        SmsMessage sms) {
        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().toString());

        // Push row into the SMS table
        Uri uri = contentResolver.insert(Uri.parse(SMS_URI), values);
        if (uri != null) {
            Log.e("", "insertion: " + uri.getPath());
        } else {
            Log.e("", "insertion: null uri");
        }
    }

问题是我没有收到短信。知道可能是什么原因吗?

没有打印任何日志。也没有在这个接收器中获得调试点。

0 个答案:

没有答案