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数据库总结:

短信数据库
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

媒体

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

http://blog.sina.com.cn/gzwncb

http://www.seewn.com

更多相关文章

  1. 完美Android Cursor使用例子(Android数据库操作)
  2. android 数据库 SQLiteOpenHelper和ContentProvider学习笔记---
  3. Android中实现Broastcast接收短信
  4. Android两种数据库操作方式入门介绍
  5. 关于Android短信管理
  6. Android Sqlite数据库转义字符模糊查询
  7. [Android] 导入外部数据库
  8. Android 使用adb shell 和android studio插件Database Navigator

随机推荐

  1. android 对sqlite数据库的增删改查
  2. Android Scroller类的详细分析
  3. 图片放大缩小
  4. Android(安卓)Logcat调试中的V、D、I、W
  5. G2 32B Android联系人重复问题修复
  6. Android线程优先级
  7. ListView中Item高度设置
  8. Appuim运行Android真机自动化测试
  9. Android apk file
  10. Android Studio 无法启动模拟器