Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

android - Block outgoing SMS by contentObserver

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)
                {}
                    }
        }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...