Andoid package text messages to gmail mailbox implementation code

Source: Internet
Author: User

The integrated backup of andriod SMS is sent to the gmail mailbox. You need to configure the gmail mailbox on the andoid mobile phone.
Github code https://github.com/zhwj184/smsbackup
View results:


You can package text messages for a few days and send them to your gmail mailbox. You can regularly back up text messages.
Main Code: Copy codeThe Code is 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 (); // obtain the 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. Cycler = 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 a 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 the recipient
EditText startDatePicker = (EditText) findViewById (R. id. startDate );
EditText endDatePicker = (EditText) findViewById (R. id. endDate );
Intent. putExtra (Intent. EXTRA_SUBJECT ,"["
+ StartDatePicker. getText (). toString () + ""
+ EndDatePicker. getText (). toString () + "] SMS backup ");
Intent. putExtra (android. content. Intent. EXTRA_TEXT, content); // sets the content
StartActivity (Intent. createChooser (intent,
"Send SMS to your mail success "));
}
}

Copy codeThe Code is 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 obtains all text message content
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 ();
}
}

For other configurations, go to 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.