Andoid package SMS sent to Gmail mailbox implementation code _android

Source: Internet
Author: User
Tags dateformat getmessage gettext
Andriod SMS Integrated backup sent to Gmail mailbox, need to andoid mobile phone configuration good Gmail mailbox
GitHub Code Https://github.com/zhwj184/smsbackup
View Effects:


Can send a few days of SMS package sent to their Gmail mailbox, you can regularly backup under the text message.
Main code:
Copy Code code as follows:

Package org.smsautobackup;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import android.app.Activity;
Import Android.app.ActivityManager;
Import Android.content.ContentResolver;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
public class Mainactivity extends activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
Date curdate = new Date (System.currenttimemillis ());//Get current time
Date lastdate = new Date (Curdate.getyear (), Curdate.getmonth (),
Curdate.getdate ()-1);
((EditText) Findviewbyid (r.id.enddate)). SetText (Formatter
. Format (curdate));
((EditText) Findviewbyid (r.id.startdate)). SetText (Formatter
. Format (lastdate));
Button btn = (button) Findviewbyid (R.id.button1);
Btn.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Sendsms (Getsmsinphone ());
}
});
Button btn1 = (button) Findviewbyid (R.id.button2);
Btn1.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
EditText txtcontent = (edittext) MainActivity.this.findViewById (R.ID.EDITTEXT1);
Autobackupservice.receiver = Txtcontent.gettext (). toString ();
StartService (New Intent (Mainactivity.this,
Autobackupservice.class));
// }
// });
}
Private String Getsmsinphone () {
StringBuilder Smsbuilder = new StringBuilder ();
EditText Startdatepicker = (edittext) Findviewbyid (r.id.startdate);
EditText Enddatepicker = (edittext) Findviewbyid (r.id.enddate);
DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
try {
Date StartDate = Df.parse (Startdatepicker.gettext (). toString ());
Date enddate = Df.parse (Enddatepicker.gettext (). toString ());
Contentresolver CR = Getcontentresolver ();
Return Smsutil.getsmsinphone (StartDate, EndDate, CR);
}catch (Exception e) {
LOG.D ("Exception in Getsmsinphone", E.getmessage ());
}
Return "";
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
protected void OnDestroy () {
Super.ondestroy ();
Activitymanager activitymgr= (Activitymanager) This.getsystemservice (Activity_service);
Activitymgr.restartpackage (Getpackagename ());
}
private void Sendsms (String content) {
Intent Intent = new Intent (Android.content.Intent.ACTION_SEND);
Intent.settype ("Plain/text");
Intent.settype ("message/rfc822"); Use this line on the real machine
EditText txtcontent = (edittext) Findviewbyid (R.ID.EDITTEXT1);
string[] Stremailreciver = new string[] {txtcontent.gettext ()
. toString ()};
Intent.putextra (Android.content.Intent.EXTRA_EMAIL, stremailreciver); Set recipient
EditText Startdatepicker = (edittext) Findviewbyid (r.id.startdate);
EditText Enddatepicker = (edittext) Findviewbyid (r.id.enddate);
Intent.putextra (Intent.extra_subject, "["
+ Startdatepicker.gettext (). toString () + "to"
+ Enddatepicker.gettext (). toString () + "SMS Backup");
Intent.putextra (Android.content.Intent.EXTRA_TEXT, content); Set content
StartActivity (Intent.createchooser (Intent,
"Send SMS to your mail Success"));
}
}

Copy Code code as follows:

Package org.smsautobackup;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Android.content.ContentResolver;
Import android.content.Intent;
Import Android.database.Cursor;
Import android.database.sqlite.SQLiteException;
Import Android.net.Uri;
Import Android.util.Log;
Import Android.widget.EditText;
public class Smsutil {
Android gets all the content of the SMS
public static String Getsmsinphone (Date startdate,date enddate,contentresolver cr) {
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";
StringBuilder Smsbuilder = new StringBuilder ();
try {
string[] projection = new string[] {"_id", "address", "person",
"Body", "date", "type"};
Uri uri = Uri.parse (Sms_uri_all);
Cursor cur = cr.query (uri, projection, null, NULL, "date desc");
if (Cur.movetofirst ()) {
String name;
String PhoneNumber;
String Smsbody;
String date;
String type;
int namecolumn = Cur.getcolumnindex ("person");
int phonenumbercolumn = Cur.getcolumnindex ("Address");
int smsbodycolumn = Cur.getcolumnindex ("Body");
int datecolumn = Cur.getcolumnindex ("date");
int typecolumn = Cur.getcolumnindex ("type");
do {
Name = Cur.getstring (NameColumn);
PhoneNumber = cur.getstring (Phonenumbercolumn);
Smsbody = cur.getstring (Smsbodycolumn);
SimpleDateFormat DateFormat = new SimpleDateFormat (
"Yyyy-mm-dd hh:mm:ss");
Date d = new Date (Long.parselong (cur.getstring (Datecolumn)));
if (D.before (startdate) | | | d.after (enddate)) {
Continue
}
Date = Dateformat.format (d);
int typeid = Cur.getint (Typecolumn);
if (typeID = = 1) {
Type = "Receive";
else if (typeID = 2) {
Type = "Send";
} else {
Type = "";
}
Smsbuilder.append ("[");
Smsbuilder.append (name==null? "": Name + ",");
Smsbuilder.append (PhoneNumber + ",");
Smsbuilder.append (Smsbody + ",");
Smsbuilder.append (date + ",");
Smsbuilder.append (type);
Smsbuilder.append ("]\n");
if (smsbody = null)
Smsbody = "";
while (Cur.movetonext ());
} else {
Smsbuilder.append ("no result!");
}
Smsbuilder.append ("Getsmsinphone has executed!");
catch (Sqliteexception ex) {
LOG.D ("Sqliteexception in Getsmsinphone", Ex.getmessage ());
}
return smsbuilder.tostring ();
}
}

Other configurations please see the GitHub.

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.