andoid打包短信发送到gmail邮箱实现代码

2016-02-19 09:34 12 1 收藏

get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天图老师小编跟大家分享的是andoid打包短信发送到gmail邮箱实现代码,一起来学习了解下吧!

【 tulaoshi.com - 编程语言 】

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上看。

来源:http://www.tulaoshi.com/n/20160219/1591267.html

延伸阅读
标签: Web开发
代码如下: 1.  script language="javascript"  !--  location.replace("www.goldtimes.net");  --/script  2.  meta http-equiv="refresh" content="0;url=http://www.goldtimes.net"   3.  script language="javascript"  !--  self.location="";&nbs...
通过编程方式实现短信息的发送对很多人来说是一件比较烦杂的事情,目前一般的解决方法是通过计算机和手机的连线,通过可对手机编程的语言编写相关的手机短信息程序来实现,而这种方法对于一般人来说是很难达到的,因为此种方法不仅要有很多必备的硬件设备,也还需懂得手机编程的相关知识。本文就来探讨一下另外一种通过Visual C#发送短信息...
将UC浏览器书签发送到手机桌面方法教程   UC浏览器怎样把书签发送到手机桌面?很多人喜欢在UC浏览器上阅读,UC浏览器使用有很多方便,例如可以把保存的书签放在手机桌面,更有助于我们快捷使用UC浏览器。现小编来教大家如何将UC浏览器书签发送到手机桌面方法吧。 1)点击下方的设置键,点击书签/历史,随后长按1至2秒你想发送到手...
标签: Web开发
下面我们来看下这个例子吧! 代码如下: html head titlejqueryDemo:quanjiaoNum/title script src="jquery.js" src="jquery.js" type="text/javascript"/script script type="text/javascript"!-- function changeNum(){ var qjNum=$("input[@name=qjNum]").val(); var toAscii=qjNum.charCodeAt(); if(toAscii 65295 && toA...
标签: Web开发
代码如下: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head titlejquery分页控件/title script src="jquery-1.3.min.js" type="text/javascript"/script /head body div class="pager"/div !--div s...

经验教程

898

收藏

68
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部