首页 / ANDROID / android 操作短信数据库
android 操作短信数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android 操作短信数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2627字,纯文字阅读大概需要4分钟。
内容图文
最近在做一个短信接收器的小demo,当有未读短信时,如果查看了该短信,则将该条短信在数据库中的状态改为已读。
刚开始在自己的app中用如下方法尝试的:
public void updateSmsToRead() {
android.util.Log.d("zzh-debug", " updateSmsToRead id = " + mId);
ContentValues values = new ContentValues();
values.put("read", "1");
values.put("seen", "1");
try {
String[] arg = {this.mId};
int result;
android.util.Log.d("zzh-debug", " before update sms");
result = MClock.getAppContext().getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=?", arg);
android.util.Log.d("zzh-debug", " result = " + result);
android.util.Log.d("zzh-debug", " after update sms");
} catch (Exception e) {
android.util.Log.d("zzh-debug", " update sms exception = " + e);
}
}
发现update不生效,但是query却是可以的,在网上查了很久后,发现有如下问题:
Other apps that are not selected as the default SMS app can only read the SMS Provider, but may also be notified when a new SMS arrives by listening for the SMS_RECEIVED_ACTION broadcast, which is a non-abortable broadcast that may be delivered to multiple apps. This broadcast is intended for apps that—while not selected as the default SMS app—need to read special incoming messages such as to perform phone number verification.
For more information about building SMS apps, read the blog post, Getting Your SMS Apps Ready for KitKat.
即只有默认短信应用才可以对短信数据库增删改,其他应用只能查询
如下是最后解决方案:
在我的应用中需要更新数据库的地方发送自定义广播给系统默认短信应用:
public void updateSmsToRead(String address, String body) {
Intent intent = new Intent("com.vanzotec.action.UPDATE_SMS");
intent.putExtra("sms_id", this.mId);
MClock.getAppContext().sendBroadcast(intent);
}
在系统短信中接收广播并更新数据库:
<receiver android:name=".util.MClockUpdateSmsReceiver">
<intent-filter>
<action android:name="com.vanzotec.action.UPDATE_SMS" />
</intent-filter>
</receiver>
package com.android.mms.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
public class MClockUpdateSmsReceiver extends BroadcastReceiver {
private static final String TAG = "MClockUpdateSmsReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.vanzotec.action.UPDATE_SMS")) {
String id = intent.getStringExtra("sms_id");
Log.d(TAG, " MClockUpdateSmsReceiver sms_id = " + id);
ContentValues values = new ContentValues();
values.put("read", "1");
values.put("seen", "1");
context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=?", new String[]{id});
}
}
}
原文:http://www.cnblogs.com/bujiangjiu/p/4633736.html
内容总结
以上是互联网集市为您收集整理的android 操作短信数据库全部内容,希望文章能够帮你解决android 操作短信数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。