Android Operation SMS Database

Source: Internet
Author: User

Recently in a small text message receiver demo, when there is an unread text message, if you view the text message, the text message in the database status changed to read.

Just start experimenting with the following methods in your 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);
}
}

Found that the update does not take effect, but query is possible, after searching the internet for a long time, found the following problems:

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.

That is, only the default SMS application can be deleted from the SMS database and other applications can only query

The final solution is as follows:

Send a custom broadcast to the system default SMS app where the database needs to be updated in my app:

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);
}

Receive the broadcast and update the database in the system SMS:

<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});
}
}

}

Android Operation SMS Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.