通过 contentObserver 阻止传出短信

发布于 2024-10-30 10:33:02 字数 2795 浏览 4 评论 0原文

我想通过 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)
                {}
                    }
        }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨离汐 2024-11-06 10:33:02

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

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.

神仙妹妹 2024-11-06 10:33:02

不,你不能。数据集修改后Observer就会生效,此时短信已经在发送中。事实上,无论如何你都无法阻止传出的短信。

Nope, you cant. Observer will comeinto affect after dataset has been modified, by that time sms will already be on the way for delivery. Infact by any means you cant block outgoing sms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文