0

可能重复:
收听传出的短信或已发送的邮箱

是否可以在 Android 中拦截传出的 SMS 消息。我知道接收 SMS 并直接发送它很简单,但是否可以检测、拦截或记录传出消息(例如,来自默认消息应用程序或来自另一个可能恶意的应用程序)?我只需要一条带有传出号码和消息的简单日志消息;我不需要修改消息。

用于此的既是合法的(监视正在发送 SMS 的应用程序)和阴暗的(监视传出消息)。我要前者而不是后者:)

4

1 回答 1

4
For **monitoring sms** state in/out you have **create class** like this 
and **start service** for **monitoring**

public class SMSMonitor 
{
    private ServiceController mainActivity;
   private ContentResolver contentResolver = null;
   private Handler smshandler = null;
   private ContentObserver smsObserver = null;
   public String smsNumber enter code here= "";
   public static boolean thCountStatus = false;
   public static int thIncreCount = 0;
   public boolean monitorStatus = false;
   String code;
//   Feedmanager fm = null;
   static public String activationCode;
   int smsCount = 0;

   public SMSMonitor(final ServiceController mainActivity, final Context mainContext) {
      this.mainActivity = mainActivity;
      contentResolver = mainActivity.getContentResolver();
      smshandler = new SMSHandler();
      smsObserver = new SMSObserver(smshandler);
   }

   public void startSMSMonitoring() {
      try {
         monitorStatus = false;
         if (!monitorStatus) {
            contentResolver.registerContentObserver(Uri
                  .parse("content://sms"), true, smsObserver);
         }
      } catch (Exception e) {
         Log.d("test","SMSMonitor :: startSMSMonitoring Exception == "+ e.getMessage());
      }
   }

   public void stopSMSMonitoring() {
      try {
         monitorStatus = false;
         if (!monitorStatus) {
            contentResolver.unregisterContentObserver(smsObserver);
         }
      } catch (Exception e) {
         Log.e("test","SMSMonitor :: stopSMSMonitoring Exception == "+ e.getMessage());
      }
   }

   class SMSHandler extends Handler {
      public void handleMessage(final Message msg) {
      }
   }

   class SMSObserver extends ContentObserver {
      private Handler sms_handle = null;
      public SMSObserver(final Handler smshandle) {
         super(smshandle);
         sms_handle = smshandle;
      }

      public void onChange(final boolean bSelfChange) {
         super.onChange(bSelfChange);
         Thread thread = new Thread() {
            public void run() {
               try {
                  monitorStatus = true;

                  // Send message to Activity
                  Message msg = new Message();
                  sms_handle.sendMessage(msg);
                  Uri uriSMSURI = Uri.parse("content://sms");
                  Cursor cur = mainActivity.getContentResolver().query(
                        uriSMSURI, null, null, null, "_id");

                  if (cur.getCount() != smsCount) {
                     smsCount = cur.getCount();

                     if (cur != null && cur.getCount() > 0) {
                        cur.moveToLast();
                        for (int i = 0; i < cur.getColumnCount(); i++) 
                        {
                           //Log("KidSafe","SMSMonitor :: incoming Column Name : " +
                              //cur.getColumnName(i));
                              //cur.getString(i));
                        }

                        smsNumber = cur.getString(cur.getColumnIndex("address"));
                        if (smsNumber == null || smsNumber.length() <= 0)
                        {
                           smsNumber = "Unknown";

                        }

                        int type = Integer.parseInt(cur.getString(cur.getColumnIndex("type")));
                        String message = cur.getString(cur.getColumnIndex("body"));
                        Log.d("test","SMSMonitor :: SMS type == " + type);
                        Log.d("test","SMSMonitor :: Message Txt == " + message);
                        Log.d("test","SMSMonitor :: Phone Number == " + smsNumber);

                        cur.close();

                        if (type == 1) {
                           onSMSReceive(message, smsNumber);
                        } else {
                           onSMSSend(message, smsNumber);
                        }
                     }
                  }
                  /*if (cur.getCount() < smsCount) {
                     Log("KidSafe","SMS Count last:: " + smsCount);
                     Log("KidSafe","SMS cur Count last:: " + cur.getCount());
                     smsCount = cur.getCount();
                     Log("KidSafe","SMS Count last:: " + smsCount);
                  }*/
               } catch (Exception e) {
//                  Log("KidSafe","SMSMonitor :: onChange Exception == "+ e.getMessage());
               }
            }
         };
         thread.start();
      }

      private void onSMSReceive(final String message, final String number) {
         synchronized (this) {
             Log.d("test", "In OnSmsReceive");
            Log.d("test", "Message"+message);
                               Log.d("Sample", "Number"+number);
         }
      }

      private void onSMSSend(final String message, final String number) {
         synchronized (this) {
             Log.d("test", "In OnSmsSend");
            Log.d("test", "Message"+message);
                               Log.d("Sample", "Number"+number);
         }
      }
   }
}

//for starting monitor create a service like below
public class ServiceController extends Service
{
   private Activity mainActivity;
   SMSMonitor sms;

   @Override
   public IBinder onBind(Intent intent) {

      return null;
   }

   public void onCreate() 
   {
      super.onCreate();

      /**** Start Listen to Outgoing SMS ****/
      Log.d("test","###### ServiceController :: CallSMS Monitor method ######");
      sms = new SMSMonitor(this , mainActivity);
      sms.startSMSMonitoring();
   }

   @Override
     public void onDestroy() {
        super.onDestroy();

        /**** Stop Listen to Outgoing SMS ****/
        Log.d("test","###### ServiceController :: StopSMS Monitor method ######");
        sms.stopSMSMonitoring();
    }
}
于 2012-07-15T06:18:26.050 回答