andoid打包短信发送到gmail邮箱实现代码
程序员文章站
2023-11-12 10:18:10
andriod短信整合备份发送到gmail邮箱,需要在andoid手机配置好gmail邮箱 github代码 https://github.com/zhwj184/smsb...
andriod短信整合备份发送到gmail邮箱,需要在andoid手机配置好gmail邮箱
github代码 https://github.com/zhwj184/smsbackup
查看效果:
可以把几天的短信打包发送到自己的gmail邮箱,可以定时备份下短信。
主要代码:
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());// 获取当前时间
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") ; // 真机上使用这行
edittext txtcontent = (edittext) findviewbyid(r.id.edittext1);
string[] stremailreciver = new string[] { txtcontent.gettext()
.tostring() };
intent.putextra(android.content.intent.extra_email, stremailreciver); // 设置收件人
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); // 设置内容
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获取短信所有内容
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 = "发送";
} 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();
}
}
其他配置请到github上看。
github代码 https://github.com/zhwj184/smsbackup
查看效果:
可以把几天的短信打包发送到自己的gmail邮箱,可以定时备份下短信。
主要代码:
复制代码 代码如下:
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());// 获取当前时间
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") ; // 真机上使用这行
edittext txtcontent = (edittext) findviewbyid(r.id.edittext1);
string[] stremailreciver = new string[] { txtcontent.gettext()
.tostring() };
intent.putextra(android.content.intent.extra_email, stremailreciver); // 设置收件人
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); // 设置内容
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获取短信所有内容
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 = "发送";
} 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();
}
}
其他配置请到github上看。
上一篇: PHP实现简单的新闻发布系统实例
下一篇: PHP简单生成缩略图相册的方法