阻止即将离任的短信通过contentObserver [英] Block outgoing SMS by contentObserver

查看:96
本文介绍了阻止即将离任的短信通过contentObserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要拦截的短信通过 contentObserver 。为此,我希望得到短信的电话号码第一。我该怎么做才能得到多少?这是code,我有,只是计算短信的数量。

 包com.SMSObserver4;
进口android.app.Activity;
进口android.database.ContentObserver;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Handler;
进口android.provider.Contacts;
进口android.provider.Contacts.People.Phones;

公共类SMSObserver4延伸活动{
        / **第一次创建活动时调用。 * /
     私有静态最后弦乐地址= NULL;
    @覆盖
        公共无效的onCreate(包savedInstanceState){
                super.onCreate(savedInstanceState);
                的setContentView(R.layout.main);
                setReceiver();
        }

        私人SmsSentCounter smsSentObserver =新SmsSentCounter(新处理器());
        私人诠释sms_sent_counter = 0;

        私人无效setReceiver(){
                this.getContentResolver()。registerContentObserver(
                                Uri.parse(内容://短信),真实,smsSentObserver);
        }
        类SmsSentCounter扩展ContentObserver {

                公共SmsSentCounter(处理程序处理){
                        超(处理);
                        // TODO自动生成构造函数存根
                }
                @覆盖
                公共无效的onChange(布尔selfChange){
                        // TODO自动生成方法存根

                    尝试{
                    的System.out.println(调用的onChange新);
                        super.onChange(selfChange);
                        光标s​​ms_sent_cursor = SMSObserver4.this.managedQuery(URI
                                        .parse(内容://短信),空,键入=?,
                                        新的String [] {2},NULL);
                        如果(sms_sent_cursor!= NULL){
                                如果(sms_sent_cursor.moveToFirst()){
                                        sms_sent_counter ++;
                                        的System.out.println(测试+ sms_sent_counter);
                                }
                        }
                        乌里phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,地址);
                        如果(phoneUri!= NULL){
                          光标phoneCursor = getContentResolver()查询(phoneUri,新的String [] {Phones._ID,Contacts.Phones.PERSON_ID},NULL,NULL,NULL);
                          如果(phoneCursor.moveToFirst()){
                            长人= phoneCursor.getLong(1); //这是你需要的人的ID
                          }
                        }
                }赶上(例外五)
                {}
                    }
        }
}
 

解决方案

我已经做了很多的测试,我发现这是不可能的。这是因为当消息应用程序中插入的短信内容提供商的新纪录,它已经尝试发送短信。所以,即使你发现短信中的内容://短信/发件箱的URI,它会来不及阻止它。事实上,有没有办法阻止它......这一切都取决于SMS应用,你不能中断。

I want to block SMS by contentObserver. For that I want to get the phone number of the SMS first. What do I do to get the number? This is the code that I have, just counting the number of SMS.

package com.SMSObserver4;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Contacts;
import android.provider.Contacts.People.Phones;

public class SMSObserver4 extends Activity {
        /** Called when the activity is first created. */
     private static final String Address = null;
    @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                setReceiver();
        }

        private SmsSentCounter smsSentObserver = new SmsSentCounter(new Handler());
        private int sms_sent_counter = 0;

        private void setReceiver() {
                this.getContentResolver().registerContentObserver(
                                Uri.parse("content://sms"), true, smsSentObserver);
        }
        class SmsSentCounter extends ContentObserver {

                public SmsSentCounter(Handler handler) {
                        super(handler);
                        // TODO Auto-generated constructor stub
                }
                @Override
                public void onChange(boolean selfChange) {
                        // TODO Auto-generated method stub

                    try{
                    System.out.println ("Calling onChange new");
                        super.onChange(selfChange);
                        Cursor sms_sent_cursor = SMSObserver4.this.managedQuery(Uri
                                        .parse("content://sms"), null, "type=?",
                                        new String[] { "2" }, null);
                        if (sms_sent_cursor != null) {
                                if (sms_sent_cursor.moveToFirst()) {
                                        sms_sent_counter++;
                                        System.out.println("test" + sms_sent_counter);
                                }
                        }
                        Uri phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Address);
                        if (phoneUri != null) {
                          Cursor phoneCursor = getContentResolver().query(phoneUri, new String[] {Phones._ID, Contacts.Phones.PERSON_ID}, null, null, null);
                          if (phoneCursor.moveToFirst()) {
                            long person = phoneCursor.getLong(1); // this is the person ID you need
                          }
                        }
                }catch(Exception e)
                {}
                    }
        }
}

解决方案

I have done a lot of tests and I found this to be impossible. That's because when the messaging application inserts a new record in the SMS content provider, it is already trying to send the SMS. So, even if you detect the SMS in the content://sms/outbox URI, it will be too late to stop it. In fact, there's no way to stop it... it all depends on the SMS application, which you can't interrupt.

这篇关于阻止即将离任的短信通过contentObserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆