2011-04-08 69 views
4

我正在尋找一種方法來掛鉤SMSManager或更低級別的機制,這樣我就可以攔截,閱讀並取消發送之前的任何傳出的SMS消息。攔截,閱讀和取消短信

+0

你想強制這種行爲還是用戶有選擇? – marsbear 2011-08-09 15:07:45

+0

被迫,但通過明智地安裝一個應用程序,它確切地說明了它的功能。 – Tom 2011-08-09 22:18:35

回答

11

很明顯,這不是你想聽到的,但是Android的設計根本不是允許第三方應用程序互相干擾或者超越用戶的自由選擇。

它沒有提供一個「hook」類型的機制來強制攔截基本功能,而不是修改安裝在設備上的Android本身的內部版本。

設備管理員界面不包含管理短信的任何內容。

是的,有時候人們會使用各種「黑客」方法在某種程度上完成這些事情,但它們與平臺的設計相悖,因此或者在不同的「錯誤修復」類型改進時不可靠或可能中斷是由android製作的。或者他們的範圍受到限制,例如更換主屏幕,從而限制輕鬆啓動的內容 - 但只有在主屏幕應用程序保持選定狀態,並且無法規範其他意圖源的情況下。

您可能會發現在頭腦的思想足夠的吸引力,你決定要運行它,但記住,除非它是一個未來的介紹打算做這種事情的API 你最有可能被利用的監督,很可能會很快糾正平臺的設計。

3

妥協怎麼樣?爲什麼不創建一個應用程序來寫短信?用戶可以選擇它作爲寫短信的默認應用程序,所以它不會一直困擾他們。我相信,只有這樣,才能實現接近您的目標而無需生根/提供您自己的Android版本。

+0

是的,我想避免這樣做,但我同意它似乎是唯一可行的方法。 – Tom 2011-08-10 11:19:21

7

猶未晚之後再也沒有:)

我把時間用在這2個天... ...而且不希望其他任何浪費自己的時間:)

  1. 撥打服務,以及註冊內容觀察者在服務類

UPDATE:加入SMS模型類

 public class SMS { 
     public Date date; 
     public String from; 
     public String message; 
     public String to; 

     public SMS(String paramString1, String paramString2, String paramString3,Date paramDate) { 
      this.from = paramString1; 
      this.to = paramString2; 
      this.message = paramString3; 
      this.date = paramDate; 
     } 
    } 

public class ServiceClass extends Service implements SMSListener { 
    public static boolean sendSms = true; 
    private final IBinder mBinder = new LocalBinder(); 
    public static MyContentObserver mSMSObserver; 
    private Context ctx; 
    public static SMS param_SMS; 
    int myID = -1; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
      // TODO Auto-generated method stub 

      myID = startId; 
      sendSms = true; 
      Context localContext = getApplicationContext(); 
      ctx = localContext; 

      mSMSObserver = new MyContentObserver(null); 
      mSMSObserver.setSMSListener(this); 
      mSMSObserver.start(localContext); 

      return Service.START_STICKY; 
    } 

     @Override 
     public void onDestroy() { 
      // TODO Auto-generated method stub 
      SharedPreferences prefs = getApplicationContext().getSharedPreferences(
        "appData", 0); 

      if (prefs.getBoolean(CommonStrings.KEY_PREFS_TOGGLE, false)) { 

       super.onDestroy(); 
       Log.e("OnDestroy", "Stopping Service"); 
       Context localContext = getApplicationContext(); 
       mSMSObserver.stop(localContext); 

       try { 
        stopSelf(myID); 
        Log.e("Stopping self", "Stopping Service"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     @Override 
     public IBinder onBind(Intent intent) { 
      // TODO Auto-generated method stub 
      Log.e("OnBinder", "OnBinder"); 
      return null; 
     } 

     @Override 
     public void reportIncomingSms(SMS paramSMS) { 
      // TODO Auto-generated method stub 

     } 

     public void reportOutgoingSms(SMS paramSMS) { 
      if (!MainActivity.stopped) { 
       Log.e("OUT GOING SMS DETECTED", "OUT GOING SMS DETECTED"); 
       sendSms = true; 
       param_SMS = paramSMS; 

       // DO ANY THING, Out going Msg detected... 

      } 
     } 

     public class LocalBinder extends Binder { 
      public LocalBinder() { 
      } 

      public ServiceClass getService() { 
       return ServiceClass.this; 
      } 
     } 
    } 
  • 擴展內容觀察員

    public class MyContentObserver extends ContentObserver { 
    
    public static final int MESSAGE_TYPE_ALL = 0; 
    public static final int MESSAGE_TYPE_DRAFT = 3; 
    public static final int MESSAGE_TYPE_FAILED = 5; 
    public static final int MESSAGE_TYPE_INBOX = 1; 
    public static final int MESSAGE_TYPE_OUTBOX = 4; 
    public static final int MESSAGE_TYPE_QUEUED = 6; 
    public static final int MESSAGE_TYPE_SENT = 2; 
    public static final String TYPE = "type"; 
    private SMSListener mSMSListener; 
    public static long _id; 
    private ContentObserver observer; 
        public MyContentObserver(Handler handler) { 
        super(handler); 
        // TODO Auto-generated constructor stub 
        } 
        private void readFromOutgoingSMS(Context paramContext) { 
        if (!MainActivity.stopped) { 
         Cursor localCursor = paramContext.getContentResolver().query(
         Uri.parse("content://sms"), null, null, null, null); 
         long l = 0; 
         int i; 
         if (localCursor.moveToNext()) { 
          l = localCursor.getLong(localCursor.getColumnIndex("_id")); 
          String str1 = localCursor.getString(localCursor 
            .getColumnIndex("protocol")); 
          i = localCursor.getInt(localCursor.getColumnIndex("type")); 
          if ((str1 != null) || (i != 6)) 
           localCursor.close(); 
           if (i == 6) { 
           int j = localCursor.getColumnIndex("body"); 
           int k = localCursor.getColumnIndex("address"); 
           Date localDate = new Date(localCursor.getLong(localCursor 
             .getColumnIndex("date"))); 
           String str2 = localCursor.getString(k); 
           String str3 = localCursor.getString(j); 
           localCursor.close(); 
            _id = l; 
            // Delete SMS and Save the sms content to custom type variable 
           if (deleteSms(paramContext, l)) { 
            SMS localSMS = new SMS("", str2, str3, localDate); 
            this.mSMSListener.reportOutgoingSms(localSMS); 
           } else { 
            localCursor.close(); 
           } 
          } 
         } 
        } 
    } 
    
    public static boolean deleteSms(Context paramContext, long paramLong) { 
        Uri localUri = ContentUris.withAppendedId(Uri.parse("content://sms"), 
          paramLong); 
        boolean bool = false; 
        if (localUri != null) { 
         try { 
          int j = paramContext.getContentResolver().delete(localUri, 
            null, null); 
          if (j == 1) 
           bool = true; 
          else 
           bool = false; 
         } catch (Exception localException) { 
          localException.printStackTrace(); 
          bool = false; 
         } 
        } 
        return bool; 
    } 
    
    private void registerContentObserver(final Context paramContext) { 
         this.observer = new ContentObserver(null) { 
           public void onChange(boolean paramAnonymousBoolean) { 
            readFromOutgoingSMS(paramContext); 
           } 
          }; 
          paramContext.getContentResolver().registerContentObserver(
           Uri.parse("content://sms"), true, this.observer); 
         } 
          public void setSMSListener(SMSListener paramSMSListener) { 
          this.mSMSListener = paramSMSListener; 
         } 
         public void start(Context paramContext) { 
          registerContentObserver(paramContext); 
          listenForIncomingSms(paramContext); 
         } 
    
         public void stop(Context paramContext) { 
          paramContext.getContentResolver().unregisterContentObserver(
            this.observer); 
         } 
         private void listenForIncomingSms(Context paramContext) { 
         //..... 
         } 
    
    } 
    
  • SMS聽者

    public abstract interface SMSListener { 
    public abstract void reportIncomingSms(SMS paramSMS); 
    
    public abstract void reportOutgoingSms(SMS paramSMS); 
        } 
    
  • 所需權限:

    <uses-permission android:name="android.permission.READ_SMS" /> 
        <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    
    +0

    我知道這是一箇舊的帖子,但是什麼是你的代碼中的「SMS」?這是一個發送短信的變量,你是如何實現的?因爲我似乎無法使您的代碼適應我的需求,因爲這是真正討論檢測傳出短信的唯一職位。這將是非常有價值的用戶遇到類似的問題,像我這樣的未來.tnx – CodeZero 2015-06-26 13:54:59

    +1

    哦:D短信只是一個模型,檢查我的更新 – 2015-06-26 15:58:55

    +0

    Tnx一百萬+1 – CodeZero 2015-06-26 16:27:06