通过contentObserver阻止传出的SMS

时间:2011-04-05 05:09:47

标签: android sms phone-number contentobserver

我想通过contentObserver阻止短信。为此,我想首先获得短信的电话号码。我该怎么做才能获得这个号码?这是我的代码,只计算短信的数量。

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)
                {}
                    }
        }
}

2 个答案:

答案 0 :(得分:6)

我做了很多测试,发现这是不可能的。这是因为当消息传递应用程序在SMS内容提供商中插入新记录时,它已经在尝试发送SMS。因此,即使您在content://sms/outbox URI中检测到SMS,停止它也为时已晚。事实上,没有办法阻止它...这一切都取决于你不能打断的短信应用程序。

答案 1 :(得分:2)

不,你不能。观察者将在修改数据集后生效,到那时短信已经在传递途中。事实上,你无法阻止传出的短信。