andoid打包短信發(fā)送到gmail郵箱實(shí)現(xiàn)代碼
github代碼 https://github.com/zhwj184/smsbackup
查看效果:


可以把幾天的短信打包發(fā)送到自己的gmail郵箱,可以定時(shí)備份下短信。
主要代碼:
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());// 獲取當(dāng)前時(shí)間
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") ; // 真機(jī)上使用這行
EditText txtContent = (EditText) findViewById(R.id.editText1);
String[] strEmailReciver = new String[] { txtContent.getText()
.toString() };
intent.putExtra(android.content.Intent.EXTRA_EMAIL, strEmailReciver); // 設(shè)置收件人
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() + "]短信備份");
intent.putExtra(android.content.Intent.EXTRA_TEXT, content); // 設(shè)置內(nèi)容
startActivity(Intent.createChooser(intent,
"send SMS to your mail success"));
}
}
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獲取短信所有內(nèi)容
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 = "接收";
} else if (typeId == 2) {
type = "發(fā)送";
} 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();
}
}
其他配置請(qǐng)到github上看。
相關(guān)文章
Android網(wǎng)絡(luò)工具類NetworkUtils詳解
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)工具類NetworkUtils,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android開(kāi)發(fā)之Notification通知用法詳解
這篇文章主要介紹了Android開(kāi)發(fā)之Notification通知用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Notification通知的功能、參數(shù)、定義及使用方法,需要的朋友可以參考下2016-11-11Android Insets相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Android Insets相關(guān)知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03Android利用Hero實(shí)現(xiàn)列表與詳情頁(yè)無(wú)縫切換動(dòng)畫
本文我們將利用Hero動(dòng)畫實(shí)現(xiàn)一個(gè)簡(jiǎn)單案例:實(shí)現(xiàn)列表與詳情頁(yè)無(wú)縫切換動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-06-06Android SharedPreferences存儲(chǔ)用法詳解
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences存儲(chǔ)用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android實(shí)現(xiàn)可瀏覽和搜索的聯(lián)系人列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可瀏覽和搜索的聯(lián)系人列表的相關(guān)代碼,瀏覽所有聯(lián)系人和根據(jù)名稱搜索聯(lián)系人,感興趣的小伙伴們可以參考一下2016-07-07基于Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問(wèn)題詳解
本篇文章對(duì)Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問(wèn)題進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09