关闭

短信备份

标签: 备份短信android
44人阅读 评论(0) 收藏 举报
分类:
package com.xh.tx.sms;

import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";
	private final String uri = "content://sms/";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		writeSMS();
		
	}

	private void readSMS() {
		ContentResolver resolver = this.getContentResolver(); //获取访问内容提供者的工具类
		String[] projection = new String[]{"_id","address","date","type","body"};
	
		Cursor cursor = resolver.query(Uri.parse(uri), projection, null, null, null);
		
		if(null != cursor && cursor.getCount() > 0)
		{
			while(cursor.moveToNext())
			{
				Integer _id = cursor.getInt(0);
				String address = cursor.getString(1);
				Long date = cursor.getLong(2);
				Integer type = cursor.getInt(3);
				String body = cursor.getString(4);
				
				Log.i(TAG, _id +" " + " address=" +address + " date = " + new Date(date) + " type=" + type + " body =" + body);
			}
			
			cursor.close();
		}
		
	}

	public void writeSMS()
	{
		ContentResolver resolver = this.getContentResolver(); //获取访问内容提供者的工具类
		
		resolver.registerContentObserver(Uri.parse(uri), true, new MyObserver(new Handler())); //内容观察者
		
		ContentValues values = new ContentValues();
		
		//desc 降序排列 limit 用来限制你返回多少条记录
		Cursor cursor = resolver.query(Uri.parse(uri), new String[]{"_id"}, null, null, " _id desc limit 1");
		
		if(null != cursor && cursor.moveToFirst())
		{
			//Log.e(TAG, "-----------------=" + cursor.getInt(0));
			//values.put("RecNo", cursor.getInt(0)+1);
			values.put("address", "95535");
			values.put("date", System.currentTimeMillis());
			values.put("type", "1");
			values.put("body", "你好啊 亲爱的!");
			
			Log.i(TAG, resolver.insert(Uri.parse(uri), values) + " ====");
		}
		
	}
	
	class MyObserver extends ContentObserver
	{

		public MyObserver(Handler handler) {
			super(handler);
			
		}
		
		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
	
			ContentResolver resolver = MainActivity.this.getContentResolver(); //获取访问内容提供者的工具类
			String[] projection = new String[]{"_id","address","date","type","body"};
			
			Cursor cursor = resolver.query(Uri.parse("content://sms/outbox"), projection, null, null, null);
			
			if(null != cursor && cursor.getCount() > 0)
			{
				while(cursor.moveToNext())
				{
					Integer _id = cursor.getInt(0);
					String address = cursor.getString(1);
					Long date = cursor.getLong(2);
					Integer type = cursor.getInt(3);
					String body = cursor.getString(4);
					
					Log.i(TAG, _id +" " + " address=" +address + " date = " + new Date(date) + " type=" + type + " body =" + body);
				}
				
				cursor.close();
		}
		}
	}
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20700次
    • 积分:1115
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:20篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论