Android短信操做(經過內容提供者)

一、Android短信數據庫表結構 java

 

URI主要有: android

content://sms/               全部短信
content://sms/inbox        收件箱
content://sms/sent          已發送
content://sms/draft         草稿
content://sms/outbox     發件箱
content://sms/failed       發送失敗
content://sms/queued    待發送列表 數據庫


sms主要結構:    app

  1. _id => 短消息序號 如100  
  2. thread_id => 對話的序號 如100  
  3. address => 發件人地址,手機號.如+8613811810000  
  4. person => 發件人,返回一個數字就是聯繫人列表裏的序號,陌生人爲null  
  5. date => 日期  long型。如1256539465022  
  6. protocol => 協議 0 SMS_RPOTO, 1 MMS_PROTO   
  7. read => 是否閱讀 0未讀, 1已讀   
  8. status => 狀態 -1接收,0 complete, 64 pending, 128 failed   
  9. type => 類型 1是接收到的,2是已發出   
  10. body => 短消息內容   
  11. service_center => 短信服務中心號碼編號。如+8613800755500

全部字段: ide

 

sms數據庫中的字段以下: this

_id               一個自增字段,從1開始
thread_id    序號,同一發信人的id相同
address      發件人手機號碼
person        聯繫人列表裏的序號,陌生人爲null 
date            發件日期
protocol      協議,分爲: 0 SMS_RPOTO, 1 MMS_PROTO  
read           是否閱讀 0未讀, 1已讀  
status         狀態 -1接收,0 complete, 64 pending, 128 failed 
type     
    ALL    = 0;
    INBOX  = 1;
    SENT   = 2;
    DRAFT  = 3;
    OUTBOX = 4;
    FAILED = 5;
    QUEUED = 6;
 
body                     短信內容
service_center     短信服務中心號碼編號
subject                  短信的主題
reply_path_present     TP-Reply-Path
locked     spa

字段源碼: .net

private void createSmsTables(SQLiteDatabase db) {
        // N.B.: Whenever the columns here are changed, the columns in
        // {@ref MmsSmsProvider} must be changed to match.
        db.execSQL("CREATE TABLE sms (" +
                   "_id INTEGER PRIMARY KEY," +
                   "thread_id INTEGER," +
                   "address TEXT," +
                   "person INTEGER," +
                   "date INTEGER," +
                   "date_sent INTEGER DEFAULT 0," +
                   "protocol INTEGER," +
                   "read INTEGER DEFAULT 0," +
                   "status INTEGER DEFAULT -1," + // a TP-Status value
                                                  // or -1 if it
                                                  // status hasn't
                                                  // been received
                   "type INTEGER," +
                   "reply_path_present INTEGER," +
                   "subject TEXT," +
                   "body TEXT," +
                   "service_center TEXT," +
                   "locked INTEGER DEFAULT 0," +
                   "error_code INTEGER DEFAULT 0," +
                   "seen INTEGER DEFAULT 0" +
                   ");");
....
}


 

二、操做代碼(拿到contentProvider的Uri便可進行增刪改查操做十分簡易)(注意:敏感數據須要申請權限android.permission.READ_SMS code

android.permission.WRITE_SMS orm

package com.pas.getsms;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import com.pas.model.SmsInfo;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.format.DateFormat;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity
{

	private List<SmsInfo> smslist;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@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;
	}

	public void click(View view)
	{
		smslist = new ArrayList<SmsInfo>();
		Uri uri = Uri.parse("content://sms/");
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(uri, new String[]
		{ "address", "date", "type", "body" }, null, null, null);
		while (cursor.moveToNext())
		{
			String address = cursor.getString(0);
			String date = cursor.getString(1);
			String type = cursor.getString(2);
			String body = cursor.getString(3);

			SmsInfo smsInfo = new SmsInfo(address, type, date, body);
			smslist.add(smsInfo);
		}
		cursor.close();
		backupData();
	}

	private void backupData()
	{
		try
		{
			String ENCODING = "utf-8";
			XmlSerializer serializer = Xml.newSerializer();
			File file = new File(this.getExternalFilesDir(null), "back.xml");
			FileOutputStream fos = new FileOutputStream(file);
			serializer.setOutput(fos, ENCODING);
			serializer.startDocument(ENCODING, true);
			serializer.startTag(null, "smses");
			for (SmsInfo sms : smslist)
			{
				serializer.startTag(null, "sms");
				
				serializer.startTag(null, "address");
				serializer.text(sms.getAddress());
				serializer.endTag(null, "address");
				
				serializer.startTag(null, "body");
				serializer.text(sms.getBody());
				serializer.endTag(null, "body");
				
				serializer.startTag(null, "date");
				serializer.text(sms.getDate());
				serializer.endTag(null, "date");
				
				serializer.startTag(null, "type");
				serializer.text(sms.getType());
				serializer.endTag(null, "type");
				
				serializer.endTag(null, "sms");
			}

			serializer.endTag(null, "smses");
			serializer.endDocument();
			fos.close();
			Toast.makeText(this, "備份成功", Toast.LENGTH_SHORT).show();
		} catch (Exception e)
		{
			e.printStackTrace();
			// Toast.makeText(this, e.getMessage(), 0).show();
		}

	}
	
	public void addSms(View view)
	{
		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					sleep(50000);
					smslist = new ArrayList<SmsInfo>();
					Uri uri = Uri.parse("content://sms/");
					ContentResolver resolver = getContentResolver();
					ContentValues values=new ContentValues();
					values.put("address", "95533");
					values.put("type", 1);
					values.put("date", System.currentTimeMillis());
					values.put("body", "到帳100,000,000元");
					resolver.insert(uri, values);
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}.start();
		
		
	}
}
相關文章
相關標籤/搜索