2011-04-05 22 views
5

SMS'i contentObserver aracılığıyla engellemek istiyorum. Bunun için önce SMS'in telefon numarasını almak istiyorum. Numarayı almak için ne yapmalıyım? Bu sahip olduğum kod, sadece SMS sayısını sayıyorum.contentObserver tarafından engellenen SMS engelleme

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

cevap

6

Çok fazla test yaptım ve bunu imkansız buldum. Çünkü mesajlaşma uygulaması SMS içerik sağlayıcısına yeni bir kayıt eklediğinde, zaten SMS göndermeye çalışıyor. Yani, SMS'i content://sms/outbox URI'de algılarsanız bile, onu durdurmak için çok geç olacaktır. Aslında, bunu durdurmanın bir yolu yok ... her şey, kesilemeyen SMS uygulamasına bağlı.

2

Hayır, yapamazsınız. Gözlemci, veri kümesi değiştirildikten sonra etkilenecektir, o zamana kadar sms teslimat için yolda olacaktır. Infact herhangi bir şekilde giden smsleri engelleyemediğiniz anlamına gelir.