短信没有保存在Kitkat 4.4 <已设置=“”as =“”default =“”messaging =“”app =“”> </已>

时间:2015-03-04 12:02:05

标签: android sms android-4.4-kitkat

我的短信应用程序不会将短信保存到设备,即使我已将其设置为默认短信。我需要支持android pre-kitkat,所以我做了一些研究,并有2个BroadcastReceiver具有相同的内容但不同的名称。 onReceive()方法工作正常,但是一旦我退出应用程序并输入它,短信就会消失。我检查了股票信息,但也没有短信。我无法弄清楚问题是什么

我的Androidmanifest:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xml>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/icon_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SmsActivity"
        android:label="@string/app_name"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.UI_OPTIONS"
            android:value="splitActionBarWhenNarrow" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".NewSmsBroadcastReceiver"
        android:permission="android.permission.BROADCAST_SMS" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
        </intent-filter>
    </receiver>
    <receiver android:name=".SmsBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"  />
        </intent-filter>
    </receiver>
    <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 android:name=".SendActivity" >
        <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
        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>

    <activity
        android:name=".SendActivity"
        android:label="@string/app_name"
        android:parentActivityName=".SmsActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.wyrmise.melriss.SmsActivity" />
    </activity>
    <activity
        android:name=".ThreadActivity"
        android:label="@string/title_activity_thread" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.wyrmise.melriss.SmsActivity" />
    </activity>
</application>

</manifest>

广播接收器

public class SmsBroadcastReceiver extends BroadcastReceiver {

public static final String SMS_BUNDLE = "pdus";

public void onReceive(Context context, Intent intent) {
    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {
        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
        String address  = "";
        String smsBody = "";

        for (int i = 0; i < sms.length; ++i) {
            SmsMessage smsMessage = SmsMessage
                    .createFromPdu((byte[]) sms[i]);
            smsBody = smsMessage.getMessageBody().toString();
            address = smsMessage.getOriginatingAddress();
        }

        Message msg = new Message();
        msg.messageNumber=address;
        msg.messageContent=smsBody;
        SimpleDateFormat hours = new SimpleDateFormat("h:mm a",
                Locale.US);
        msg.messageDate=hours.format(new Date());

        SmsActivity inst = SmsActivity.instance();
        inst.pushNotification(msg);
        inst.updateList(msg);
    }
}
}

1 个答案:

答案 0 :(得分:3)

默认的SMS应用程序负责将所有传入的消息写入提供者。即使您为应用程序实现了Receiver以从SMS_RECEIVED广播中读取消息,您仍然需要将消息实际写入提供者。例如:

ContentValues values = new ContentValues();
values.put(Sms.ADDRESS, address);
values.put(Sms.BODY, smsBody);
context.getContentResolver().insert(Sms.CONTENT_URI, values);