1.android發送短信
android API 中提供了smsManager類處理短信。其中的sendTextMessage(num, null, content, pend, null)函數就是發送
短信的方法。第一個參數爲目標者手機號、第二個參數爲短信中心地址 null爲默認地址、
第三個參數短信的文本內容、第四個參數是一個intent會把發送結果帶回。第五個參數不知,通常爲null。
一個應用程序要具有發送短信功能,須要在androidManifest.xml中加入android.permission.SEND_SMS權限。
在模擬器中發送中文會接收方出現亂碼的問題,可是在真機中,就不會出現亂碼的狀況了。因此
開發者只須要正常開發短信功能,不須要編碼轉換。
接收短信也是比較方便的,主要是繼承BroadcaseReceiver 類 ,覆蓋onReceive 函數:
1:相關類:
android.content.BroadcastReceiver
android.telephony.gsm.SmsMessage;
2:example code.
public class MessageDemo extends BroadcastReceiver {
private static final String strACT = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(strACT)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msg = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage currMsg : msg) {
sb.append("From:");
sb.append(currMsg.getDisplayOriginatingAddress());
sb.append("\nMessage:");
sb.append(currMsg.getDisplayMessageBody());
}
}
}
}
}
3: 相關的配置
修改AndroidManifest.xml,在Activity下添加receiver節點:
<receiver android:name="MessageDemo">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
隨後在application下添加節點:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
4:使用BroadReceiver的弊端
查看BroadReceiver sdk reference , 能夠了解到全部的BroadReceiver對短信的接收是無順序的狀態 ,即便是使用了Ordered broadcasts對於同等優先級別的BroadReceiver ,也會產生無順序的行爲。
因此下面介紹另外一種接收短信的行爲,包括其中能夠進行短信的刪除。
5:從數據庫端監聽sms的收發
//以下 主要用於內部數據庫改變,向外面的界面(Activity)作反應
class SMSHandler extends Handler
{
public void handleMessage(Message msg)
{
//Handle message
}
}
// 對收到短消息後,作出的處理,這裏直接刪除,並無反應到界面,因此上面的handleMessage是空的。
class SMSObserver extends ContentObserver
{
private Handler m_handle = null;
public SMSObserver(Handler handle)
{
super(handle);
m_handle = handle;
}
public void onChange(boolean bSelfChange)
{
super.onChange(bSelfChange);
//Send message to Activity
Message msg = new Message();
msg.obj = "xxxxxxxxxx";
m_handle.sendMessage(msg);
String strUriInbox = "content://sms/inbox";
Uri uriSms = Uri.parse(strUriInbox); //If you want to access all SMS, just replace the uri string to "content://sms/"
Cursor c = mContext.getContentResolver().query(uriSms, null, null, null, null);
// delete all sms here when every new sms occures.
while (c.moveToNext())
{
//Read the contents of the SMS;
for(int i; i < c.getColumnCount(); i++)
{
String strColumnName = c.getColumnName(i);
String strColumnValue = c.getString(i);
}
//Delete the SMS
String pid = c.getString(1); //Get thread id;
String uri = "content://sms/conversations/" + pid;
mContext.getContentResolver().delete(Uri.parse(uri), null, null);
}
}
}
//把基本類功能性地應用起來
ContentResolver contentResolver = getContentResolver();// Context 環境下getContentResolver()
Handler handler = new SMSHandler();
ContentObserver m_SMSObserver = new SMSObserver(handler);
contentResolver.registerContentObserver(Uri.parse("content://sms/inbox"),true, m_SMSObserver);
//Register to observe SMS in outbox,we can observe SMS in other location by changing Uri string, such as inbox, sent, draft, outbox, etc.)
// some Available Uri string for sms.
/*
String strUriInbox = "content://sms/inbox";//SMS_INBOX:1
String strUriFailed = "content://sms/failed";//SMS_FAILED:2
String strUriQueued = "content://sms/queued";//SMS_QUEUED:3
String strUriSent = "content://sms/sent";//SMS_SENT:4
String strUriDraft = "content://sms/draft";//SMS_DRAFT:5
String strUriOutbox = "content://sms/outbox";//SMS_OUTBOX:6
String strUriUndelivered = "content://sms/undelivered";//SMS_UNDELIVERED
String strUriAll = "content://sms/all";//SMS_ALL
String strUriConversations = "content://sms/conversations";//you can delete one conversation by thread_id
String strUriAll = "content://sms"//you can delete one message by _id
*/
REMEBER: must request following permission
1) Read SMS
<uses-permssion android:name="android.permission.READ_SMS" />
2) Delete/Modify/Send SMS
<uses-permssion android:name="android.permission.WRITE_SMS" />
in AndroidManifest.xml android
Android數據庫總結: 數據庫
短信數據庫
String strUriInbox = "content://sms";
Uri uriSms = Uri.parse(strUriInbox);
Cursor c_groups = managedQuery( uriSms , new String[] { "date","person" }, select, null, "date DESC");
strColumnName=_id strColumnValue=48 //短消息序號
strColumnName=thread_id strColumnValue=16 //對話的序號(conversation)
strColumnName=address strColumnValue=+8610086 //發件人地址,手機號
strColumnName=person strColumnValue=null //發件人,返回一個數字就是聯繫人列表裏的序號,陌生人爲null
strColumnName=date strColumnValue=1256539464222 //日期 long型,想獲得具體日期本身轉換吧!
strColumnName=protocol strColumnValue=0 //協議
strColumnName=read strColumnValue=1 //是否閱讀
strColumnName=status strColumnValue=-1 //狀態
strColumnName=type strColumnValue=1 //類型 1是接收到的,2是發出的
strColumnName=reply_path_present strColumnValue=0 //
strColumnName=subject strColumnValue=null //主題
strColumnName=body strColumnValue=您好 //短消息內容
strColumnName=service_center strColumnValue=+8613800755500 //短信服務中心號碼編號,能夠得知該短信是從哪裏發過來的
2.聯繫人數據庫
strColumnName = _sync_id strColumnValue=null
strColumnName = primary_organization strColumnValue=null
strColumnName = notes strColumnValue=null
strColumnName = primary_phone strColumnValue=1
strColumnName = status strColumnValue=null
strColumnName = im_handle strColumnValue=null
strColumnName = _sync_local_id strColumnValue=null
strColumnName = im_account strColumnValue=null
strColumnName = _sync_time strColumnValue=null
strColumnName = im_protocol strColumnValue=null
strColumnName = mode strColumnValue=null
strColumnName = label strColumnValue=null
strColumnName = times_contacted strColumnValue=0
strColumnName = name strColumnValue=null
strColumnName = send_to_voicemail strColumnValue=null
strColumnName = primary_email strColumnValue=null
strColumnName = custom_ringtone strColumnValue=null
strColumnName = sort_string strColumnValue=null
strColumnName = _sync_version strColumnValue=null
strColumnName = last_time_contacted strColumnValue=null
strColumnName = _sync_account strColumnValue=null
strColumnName = display_name strColumnValue=null
strColumnName = number_key strColumnValue=77681111831
strColumnName = number strColumnValue=13811112345
strColumnName = phonetic_name strColumnValue=null
strColumnName = _id strColumnValue=1
strColumnName = type strColumnValue=2
strColumnName = _sync_dirty strColumnValue=1
strColumnName = starred strColumnValue=0
4.其餘數據庫
//Available Uri string
content://contacts/people //本地聯繫人列表信息
content://contacts/phones //本地聯繫人列表信息
content://call_log/calls/ //本地通話記錄
content://mms 彩信
content://mms-sms/threadID
content://mms-sms/conversations
content://mms-sms/messages/byphone
content://mms-sms/undelivered
content://mms-sms/draft app
媒體 ide
content://media/internal/images 這個URI將返回設備上存儲的全部圖片
String strUriInbox = "content://sms/inbox"; //SMS_INBOX:1
String strUriFailed = "content://sms/failed"; //SMS_FAILED:2
String strUriQueued = "content://sms/queued"; //SMS_QUEUED:3
String strUriSent = "content://sms/sent"; //SMS_SENT:4
String strUriDraft = "content://sms/draft"; //SMS_DRAFT:5
String strUriOutbox = "content://sms/outbox"; //SMS_OUTBOX:6
String strUriUndelivered = "content://sms/undelivered"; //SMS_UNDELIVERED
String strUriAll = "content://sms/all"; //SMS_ALL
String strUriConversations= "content://sms/conversations";//you can delete one conversation by thread_id
String strUriAll = "content://sms" //you can delete one message by _id 函數
訪問短信數據庫的uri
content://sms/inbox 收件箱
content://sms/sent 已發送
content://sms/draft 草稿
content://sms/outbox 發件箱
content://sms/failed 發送失敗
content://sms/queued 待發送列表
數據庫相關字段以下:
_id 一個自增字段,從1開始
thread_id 序號,同一發信人的id相同
address 發件人手機號碼(根據這個查找聯繫人姓名?)
person 聯繫人列表裏的序號,陌生人爲null
date 發件日期,單位是milliseconds,從1970/01/01至今所通過的時間)
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訪問短信數據庫的uri
content://sms/inbox 收件箱
content://sms/sent 已發送
content://sms/draft 草稿
content://sms/outbox 發件箱
content://sms/failed 發送失敗
content://sms/queued 待發送列表
content://sms/conversations"; //you can delete one conversation by thread_id
數據庫相關字段以下:
_id 一個自增字段,從1開始
thread_id 序號,同一發信人的id相同
address 發件人手機號碼(根據這個查找聯繫人姓名?)
person 聯繫人列表裏的序號,陌生人爲null
date 發件日期,單位是milliseconds,從1970/01/01至今所通過的時間)
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 ui