Android read SMS Sample share _android

Source: Internet
Author: User
Tags dateformat

Copy Code code as follows:

Package com.homer.sms;

Import Java.sql.Date;
Import Java.text.SimpleDateFormat;


Import android.app.Activity;
Import Android.database.Cursor;
Import android.database.sqlite.SQLiteException;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.widget.ScrollView;
Import Android.widget.TableLayout;
Import Android.widget.TextView;

public class Smsread extends activity {

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

TextView TV = new TextView (this);
Tv.settext (Getsmsinphone ());

ScrollView SV = new ScrollView (this);
Sv.addview (TV);

Setcontentview (SV);
}

Public String Getsmsinphone () {
Final String Sms_uri_all = "content://sms/";
Final String Sms_uri_inbox = "Content://sms/inbox";
Final String sms_uri_send = "Content://sms/sent";
Final String Sms_uri_draft = "Content://sms/draft";
Final String Sms_uri_outbox = "Content://sms/outbox";
Final String sms_uri_failed = "content://sms/failed";
Final String sms_uri_queued = "content://sms/queued";

StringBuilder Smsbuilder = new StringBuilder ();

try {
Uri uri = Uri.parse (Sms_uri_all);
string[] projection = new string[] {' _id ', ' address ', ' person ', ' body ', ' date ', ' type '};
Cursor cur = getcontentresolver (). Query (URI, projection, null, NULL, "date desc"); Get SMS inside Mobile phone

if (Cur.movetofirst ()) {
int index_address = Cur.getcolumnindex ("Address");
int Index_person = Cur.getcolumnindex ("person");
int index_body = Cur.getcolumnindex ("Body");
int index_date = Cur.getcolumnindex ("Date");
int index_type = Cur.getcolumnindex ("Type");

do {
String straddress = cur.getstring (index_address);
int Intperson = Cur.getint (Index_person);
String strbody = cur.getstring (index_body);
Long longdate = Cur.getlong (index_date);
int inttype = Cur.getint (Index_type);

SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
Date d = new Date (longdate);
String strdate = Dateformat.format (d);

String strtype = "";
if (inttype = = 1) {
Strtype = "Receive";
else if (Inttype = 2) {
strtype = "Send";
} else {
Strtype = "NULL";
}

Smsbuilder.append ("[");
Smsbuilder.append (straddress + ",");
Smsbuilder.append (Intperson + ",");
Smsbuilder.append (Strbody + ",");
Smsbuilder.append (strdate + ",");
Smsbuilder.append (strtype);
Smsbuilder.append ("]\n\n");
while (Cur.movetonext ());

if (!cur.isclosed ()) {
Cur.close ();
cur = null;
}
} else {
Smsbuilder.append ("no result!");
}//End If

Smsbuilder.append ("Getsmsinphone has executed!");

catch (Sqliteexception ex) {
LOG.D ("Sqliteexception in Getsmsinphone", Ex.getmessage ());
}

return smsbuilder.tostring ();
}
}



Remember to join Android.permission.READ_SMS this permission in Androidmanifest.xml.
Copy Code code as follows:

<uses-permission android:name= "Android.permission.READ_SMS"/>

Related Article

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.