短信在 Kitkat 4.4 上不保存 <Already set as default messaging app>
Sms doesn't save on Kitkat 4.4 <Already set as default messaging app>
我的短信应用程序没有将短信保存到设备,即使我已经将其设置为默认短信。我需要支持 android pre-kitkat,所以我做了一些研究,得到了 2 个内容相同但名称不同的 BroadcastReceiver。 onReceive() 方法工作正常,但一旦我退出应用程序并进入它,短信就会消失。我查看了股票消息,但也没有短信。我不知道是什么问题
我的 Android 清单:
<?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);
}
}
}
即使您已将接收器设置为接收系统广播,您仍然需要存储从它们检索到的消息;例如:
ContentValues values = new ContentValues();
values.put(Telephony.Sms.Inbox.ADDRESS, smsMessage.getDisplayOriginatingAddress());
values.put(Telephony.Sms.Inbox.BODY, smsBody);
values.put(Telephony.Sms.Inbox.DATE, System.currentTimeMillis());
values.put(Telephony.Sms.Inbox.PROTOCOL, smsMessage.getProtocolIdentifier());
values.put(Telephony.Sms.Inbox.READ, 0);
values.put(Telephony.Sms.Inbox.SEEN, 0);
values.put(Telephony.Sms.Inbox.SERVICE_CENTER, smsMessage.getServiceCenterAddress());
values.put(Telephony.Sms.THREAD_ID, Telephony.Threads.getOrCreateThreadId(context, address));
Uri insertUri = context.getContentResolver().insert(Telephony.Sms.Inbox.CONTENT_URI, values);
从 KitKat 开始,默认的 SMS 应用程序负责将所有传入消息写入 Provider,因此当您的应用程序是这些版本的默认应用程序时,您肯定需要保存来自 SMS_DELIVER
广播的消息.
KitKat 之前,没有默认的 SMS 应用程序,因此用户必须确保至少有一个消息传递应用程序,通常是预安装的,可以自动处理保存消息,但它很好一场混战。如果您支持这些版本,您可能也需要处理插入消息的问题,如果没有其他应用程序在做的话。
我的短信应用程序没有将短信保存到设备,即使我已经将其设置为默认短信。我需要支持 android pre-kitkat,所以我做了一些研究,得到了 2 个内容相同但名称不同的 BroadcastReceiver。 onReceive() 方法工作正常,但一旦我退出应用程序并进入它,短信就会消失。我查看了股票消息,但也没有短信。我不知道是什么问题
我的 Android 清单:
<?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);
}
}
}
即使您已将接收器设置为接收系统广播,您仍然需要存储从它们检索到的消息;例如:
ContentValues values = new ContentValues();
values.put(Telephony.Sms.Inbox.ADDRESS, smsMessage.getDisplayOriginatingAddress());
values.put(Telephony.Sms.Inbox.BODY, smsBody);
values.put(Telephony.Sms.Inbox.DATE, System.currentTimeMillis());
values.put(Telephony.Sms.Inbox.PROTOCOL, smsMessage.getProtocolIdentifier());
values.put(Telephony.Sms.Inbox.READ, 0);
values.put(Telephony.Sms.Inbox.SEEN, 0);
values.put(Telephony.Sms.Inbox.SERVICE_CENTER, smsMessage.getServiceCenterAddress());
values.put(Telephony.Sms.THREAD_ID, Telephony.Threads.getOrCreateThreadId(context, address));
Uri insertUri = context.getContentResolver().insert(Telephony.Sms.Inbox.CONTENT_URI, values);
从 KitKat 开始,默认的 SMS 应用程序负责将所有传入消息写入 Provider,因此当您的应用程序是这些版本的默认应用程序时,您肯定需要保存来自 SMS_DELIVER
广播的消息.
KitKat 之前,没有默认的 SMS 应用程序,因此用户必须确保至少有一个消息传递应用程序,通常是预安装的,可以自动处理保存消息,但它很好一场混战。如果您支持这些版本,您可能也需要处理插入消息的问题,如果没有其他应用程序在做的话。