短信没有显示在收件箱中

时间:2014-07-04 19:17:27

标签: android sms broadcastreceiver android-broadcast

我正在创建短信应用程序,这里是短信接收器和AndroidManifest.xml的代码: -

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;

import com.arays.sms.MainActivity;
import com.arays.sms.R;

public class SmsReceiver extends BroadcastReceiver {

    ArrayList<String> your_array_list = new ArrayList<String>();
    @SuppressLint("NewApi")
    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        String str2 = "";
        if (bundle != null) {
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                str2 += msgs[i].getOriginatingAddress();
                // str2 += " :";
                str += msgs[i].getMessageBody().toString();
            }

            Uri lookupUri = Uri.withAppendedPath(
                    PhoneLookup.CONTENT_FILTER_URI, Uri.encode(str2));
            Cursor c = context.getContentResolver().query(lookupUri,
                    new String[] { ContactsContract.Data.DISPLAY_NAME }, null,
                    null, null);
            try {
                c.moveToFirst();
                String displayName = c.getString(0);
                str2 = displayName;

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                c.close();
            }

            Intent i = new Intent(context, MainActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(context, 0, i, 0);

            String uri = "tel:" + str2;
            Intent iph = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
            PendingIntent pCall = PendingIntent.getActivity(context, 0, iph, 0);

            // Build notification

            Notification noti = new Notification.Builder(context)
                    .setStyle(new Notification.BigTextStyle().bigText(str))
                    .setContentTitle("New sms from " + str2)
                    .setContentText(str)
                    .setSmallIcon(R.drawable.ic_action_chat)
                    .setContentIntent(pIntent)
                    .addAction(R.drawable.ic_action_call, "Call", pCall)
                    .addAction(R.drawable.ic_action_accept, "Read", pIntent)
                    .addAction(R.drawable.ic_action_discard, "Delete", pIntent)
                    .build();

            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(context.NOTIFICATION_SERVICE);
            // hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;
            // notification
            notificationManager.notify(0, noti);
        }
    }


    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.arays.sms"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="20" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.WRITE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.arays.sms.MainActivity"
            android:label="@string/app_name"
            android:parentActivityName="com.arays.sms.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.arays.sms.fragments.MessagesFragment"
            android:label="MessagesFragment" >
        </activity>
        <activity
            android:name="com.arays.sms.fragments.ContactsFragment"
            android:label="ContactsFragment"
            android:parentActivityName="com.arays.sms.MainActivity" >
        </activity>

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

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver
            android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH" >
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />

                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service
            android:name=".HeadlessSmsSendService"
            android:exported="true"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>
    </application>

</manifest>

但问题是当我将我的应用程序设置为默认短信应用程序时,它不会在收件箱中显示短信。我收到短信文本和号码的通知,但我的应用程序的收件箱或手机上的任何其他短信应用程序中都没有msg。我在这里做错了什么帮助。我是否需要更新接收器中的短信数据库。

谢谢

1 个答案:

答案 0 :(得分:0)

找到它

ContentValues contentvalues = new ContentValues();
        contentvalues.put("address", str2);
        contentvalues.put("body", str);
        contentvalues.put("seen", Integer.valueOf(1));
        context.getContentResolver().insert(Uri.parse("content://sms/inbox"), contentvalues)