andoid 패키지 문자를 gmail 메일로 보내기

6680 단어
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("]
"); 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에서 보십시오.

좋은 웹페이지 즐겨찾기