android 短信接收

接收短信也是比较方便的,主要是继承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 Handle m_handle = null;

    public SMSObserver(Handle 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 uri = "content://sms";
       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"),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




你可能感兴趣的:(C++,c,xml,android,C#)