基於android 社區app簡訊分享 發送回調事件實現

來源:互聯網
上載者:User

標籤:android   app   簡訊分享   回調   

概要

前段時間,因為項目的需要,使用ShareSDK的分享功能,其中包括 簡訊分享嗎,並且在調用系統簡訊分享成功後要與伺服器進行互動處理(我這裡不關心,對方能否收到,只關心發出去了)。但是ShareSDk並不支援簡訊分享的回調功能,諮詢了技術客服,也沒有討論出解決方案來。於是自己嘗試粗略實現了一下。

方法

在調用系統簡訊發送後,通過內容觀察者監聽 簡訊寄件匣的改變,如果監聽到有內容改變的簡訊ID,那麼擷取當前的內容並檢測其中是否含有某些關鍵字( 當然,這個關鍵字是我們自己定義的,例如:“jarlen”);如果找到則說明已發送了(我這裡不關心,對方能否收到)。

核心 Code
/** * Created by jarlen on 2015/6/4. */public class SMSContentObserver extends ContentObserver {    private Context mContext;    private boolean isGoing = false;    private Handler mHandler;    private String targetAddress = null;    private String observerContent = null;    /**     * 簡訊息發送監聽者構造器     *     * @param context     * @param handler 監聽回調     * @param address 監聽的目標手機號     * @param content 監聽的內容關鍵字     */    public SMSContentObserver(Context context, Handler handler, String address, String content) {        super(handler);        this.mContext = context;        this.mHandler = handler;        if (address != null) {            //去掉手機號中的全部空格            this.targetAddress = address.replaceAll(" ", "");        }        this.observerContent = content;    }    Object obj = new Object();    @Override    public void onChange(boolean selfChange) {        synchronized (obj) {            if (!isGoing) {                isGoing = true;                Cursor cursor = mContext.getContentResolver().query(                        Uri.parse("content://sms/outbox"), null, null, null,                        null);                String address = null;                String smsContent = null;                // 遍曆查詢結果擷取使用者正在發送的簡訊                while (cursor.moveToNext()) {                    StringBuffer sb = new StringBuffer();                    // 擷取簡訊的發送地址                    address = cursor                            .getString(cursor.getColumnIndex("address"));                    smsContent = cursor                            .getString(cursor.getColumnIndex("body"));                }                if (address != null && smsContent != null) {                    // 找到一個正在發送的簡訊                    Log.e("===", "找到一個正在發送的簡訊");                    if (targetAddress != null) {                        // 事先指定的收件者不為空白                        if (address.contains(targetAddress)                                && smsContent.contains(observerContent)) {                            // 正好是事先指定的收件者,並且資訊內容含有某種關鍵字                            Log.e("===", "資訊內容含有某種關鍵字");                            Message msg = mHandler.obtainMessage();                            msg.obj = address;                            msg.what = 1;                            msg.sendToTarget();                        } else {                            Message msg = mHandler.obtainMessage();                            msg.what = 0;                            msg.sendToTarget();                        }                    } else {                        // 事先沒有指定的收件者                        if (smsContent.contains(observerContent)) {                            // 資訊內容含有某種關鍵字                            Log.e("===", "資訊內容含有某種關鍵字");                            Message msg = mHandler.obtainMessage();                            msg.obj = address;                            msg.what = 1;                            msg.sendToTarget();                        } else {                            Message msg = mHandler.obtainMessage();                            msg.what = 0;                            msg.sendToTarget();                        }                    }                }            }        }    }}
建立監聽
    /**     * 監聽     */    private SMSContentObserver smsContentObserver;    private boolean smsContentObserverFind = false;    private Handler mHandler = new Handler() {    public void handleMessage(Message msg) {            if (msg.what == 1 && !smsContentObserverFind) {                ........// 相關處理                smsContentObserverFind = true;            }        }    };
smsContentObserver = new SMSContentObserver(this, mHandler, usernumber,"某關鍵詞");getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, smsContentObserver);
/***監聽解除綁定*/if (smsContentObserver != null) {            getContentResolver().unregisterContentObserver(smsContentObserver);            smsContentObserver = null;        }

基於android 社區app簡訊分享 發送回調事件實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.