Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测传出的sms android_Android_Sms_Contentobserver - Fatal编程技术网

检测传出的sms android

检测传出的sms android,android,sms,contentobserver,Android,Sms,Contentobserver,我正在使用ContentObserver检测传出消息。问题在于,每当发送消息时,onChange()都会触发三次。我一直在看关于SO的一些答案,这似乎是因为每次发送消息时都会更新三个表 幸运的是,每条消息都有一个唯一的id,但我不太明白如何最好地过滤出具有相同id的消息。我尝试了一些不同的方法,但它们似乎都失败了 有人知道这样做的好方法吗 我的观察者看起来像这样 private class OutgoingSMS extends ContentObserver { public

我正在使用ContentObserver检测传出消息。问题在于,每当发送消息时,onChange()都会触发三次。我一直在看关于SO的一些答案,这似乎是因为每次发送消息时都会更新三个表

幸运的是,每条消息都有一个唯一的id,但我不太明白如何最好地过滤出具有相同id的消息。我尝试了一些不同的方法,但它们似乎都失败了

有人知道这样做的好方法吗

我的观察者看起来像这样

    private class OutgoingSMS extends ContentObserver {
    public OutgoingSMS(Handler handler) {
        super(handler);
    }

    @Override
    public boolean deliverSelfNotifications(){
        return true;
    }

    @Override
    public void onChange(boolean selfChange){
        super.onChange(selfChange);

        Uri smsuri = Uri.parse("content://sms/sent");
        Cursor cursor = mContext.getContentResolver().query(smsuri, null, null, null, null);
        String outgoingSMS = null;

        if (cursor != null && cursor.moveToFirst()){

            try{

                cursor.moveToFirst();
                String type = cursor.getString(cursor.getColumnIndex("type"));

                if (type.equals("2")){

                    outgoingSMS = cursor.getString(cursor.getColumnIndex("address"));
                    String subString = outgoingSMS.substring(0, 3);
                }

            }
            catch (CursorIndexOutOfBoundsException e){
                Log.e(Constants.TAG, "SMSHelper: outgoingSMS", e);
            }
            finally{
                cursor.close();
            }

            //Filter out duplicate ids here.

            if (outgoingSMS != null ){
                long timestamp= getTime();
                DataModel.getInstance(mContext).addLog(outgoingSMS), "sms", timestamp, 0, 1);
                Log.d(Constants.TAG, "SMSHelper: outgoingSMS: " + outgoingSMS);

            }
        }
    }
}
您可以参考以下内容:[[1]: