欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?ContentObserver?監(jiān)聽短信思路詳解

 更新時間:2024年09月29日 10:04:43   作者:大風起兮云飛揚丶  
ContentObserver允許在Android中監(jiān)控特定數(shù)據(jù)的變化,可用于短信等應(yīng)用的數(shù)據(jù)監(jiān)聽,開發(fā)者可通過繼承ContentObserver并實現(xiàn)onChange方法來定義當目標內(nèi)容變化時的響應(yīng)行為,感興趣的朋友一起看看吧

概述 

內(nèi)容觀察器ContentObserver給目標內(nèi)容注冊一個觀察器,目標內(nèi)容的數(shù)據(jù)一旦發(fā)生變化,觀察器規(guī)定好的動作馬上觸發(fā),從而執(zhí)行開發(fā)者預(yù)先定義的代碼。

思路

注冊一個監(jiān)聽

getContentResolver().registerContentObserver(uri, true, mObserver);

繼承 ContentObserver 實現(xiàn)一個用于回調(diào)的監(jiān)聽類

 private static class SmsGetObserver extends ContentObserver {
        private final Context mContext;
        public SmsGetObserver(Context context) {
            super(new Handler(Looper.getMainLooper()));
            this.mContext = context;
        }
        @SuppressLint("Range")
        @Override
        public void onChange(boolean selfChange, @Nullable Uri uri) {
            super.onChange(selfChange, uri);
            // onChange會多次調(diào)用,收到一條短信會調(diào)用兩次onChange
            // mUri===content://sms/raw/20
            // mUri===content://sms/inbox/20
            // 安卓7.0以上系統(tǒng),點擊標記為已讀,也會調(diào)用一次
            // mUri===content://sms
            // 收到一條短信都是uri后面都會有確定的一個數(shù)字,對應(yīng)數(shù)據(jù)庫的_id,比如上面的20
            Log.d("aabb",uri.toString());
            if (uri == null) {
                return;
            }
            if (uri.toString().contains("content://sms/raw") ||
                    uri.toString().equals("content://sms")) {
                return;
            }
            // 通過內(nèi)容解析器獲取符合條件的結(jié)果游標集
            Cursor cursor = mContext.getContentResolver().query(uri, new String[]{"address", "body", "date"}, null, null, "date Desc");
            if (cursor.moveToNext()) {
                // 短信的發(fā)送號碼
                String sender = cursor.getString(cursor.getColumnIndex("address"));
                // 短信內(nèi)容
                String content = cursor.getString(cursor.getColumnIndex("body"));
                Log.d("AAAA", String.format("sender:%s,content:%s", sender, content));
            }
            cursor.close();
        }
    }

短信相關(guān)權(quán)限

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

完整代碼

package com.example.cpclient;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
public class MonitorSmsActivity extends AppCompatActivity {
    private SmsGetObserver mObserver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor_sms);
        // 給指定Uri注冊內(nèi)容觀察器,一旦發(fā)生數(shù)據(jù)變化,就觸發(fā)觀察器的onChange方法
        Uri uri = Uri.parse("content://sms");
        // notifyForDescendents:
        // false :表示精確匹配,即只匹配該Uri,true :表示可以同時匹配其派生的Uri
        // 假設(shè)UriMatcher 里注冊的Uri共有以下類型:
        // 1.content://AUTHORITIES/[table]
        // 2.content://AUTHORITIES/[table]/#
        // 3.content://AUTHORITIES/[table]/[subtable]
        // 假設(shè)我們當前需要觀察的Uri為content://AUTHORITIES/student:
        // 如果發(fā)生數(shù)據(jù)變化的 Uri 為 3。
        // 當notifyForDescendents為false,那么該ContentObserver會監(jiān)聽不到,但是當notifyForDescendents 為ture,能捕捉該Uri的數(shù)據(jù)庫變化。
        mObserver = new SmsGetObserver(this);
        getContentResolver().registerContentObserver(uri, true, mObserver);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(mObserver);
    }
    private static class SmsGetObserver extends ContentObserver {
        private final Context mContext;
        public SmsGetObserver(Context context) {
            super(new Handler(Looper.getMainLooper()));
            this.mContext = context;
        }
        @SuppressLint("Range")
        @Override
        public void onChange(boolean selfChange, @Nullable Uri uri) {
            super.onChange(selfChange, uri);
            // onChange會多次調(diào)用,收到一條短信會調(diào)用兩次onChange
            // mUri===content://sms/raw/20
            // mUri===content://sms/inbox/20
            // 安卓7.0以上系統(tǒng),點擊標記為已讀,也會調(diào)用一次
            // mUri===content://sms
            // 收到一條短信都是uri后面都會有確定的一個數(shù)字,對應(yīng)數(shù)據(jù)庫的_id,比如上面的20
            Log.d("aabb",uri.toString());
            if (uri == null) {
                return;
            }
            if (uri.toString().contains("content://sms/raw") ||
                    uri.toString().equals("content://sms")) {
                return;
            }
            // 通過內(nèi)容解析器獲取符合條件的結(jié)果游標集
            Cursor cursor = mContext.getContentResolver().query(uri, new String[]{"address", "body", "date"}, null, null, "date Desc");
            if (cursor.moveToNext()) {
                // 短信的發(fā)送號碼
                String sender = cursor.getString(cursor.getColumnIndex("address"));
                // 短信內(nèi)容
                String content = cursor.getString(cursor.getColumnIndex("body"));
                Log.d("AAAA", String.format("sender:%s,content:%s", sender, content));
            }
            cursor.close();
        }
    }
}

拓展

當在你的provider中,別人insert了一條數(shù)據(jù),你要告知他是否成功了

使用 notifyChange進行通知回調(diào)

getContext().getContentResolver().notifyChange()

案例

   @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (URI_MATCHER.match(uri) == USERS) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            long rowId = db.insert(UserDBHelper.TABLE_NAME, null, values);
            if (rowId > 0) {
                // 如果添加成功,就利用新記錄的行號生成新的地址
                Uri newUri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI, rowId);
                // 通知監(jiān)聽器,數(shù)據(jù)已經(jīng)改變
                getContext().getContentResolver().notifyChange(newUri, null);
            }
        }
        return uri;
    }

案例代碼

到此這篇關(guān)于Android ContentObserver 監(jiān)聽短信的文章就介紹到這了,更多相關(guān)Android ContentObserver 監(jiān)聽短信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論