在Android上记录外发短信

时间:2012-07-15 04:19:37

标签: android

  

可能重复:
  Listen to outgoing SMS or sent box

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

这种用途都是合法的(监控应用程序正在发送短信)和阴暗(间接传出消息)。我要去前者而不是后者:)

1 个答案:

答案 0 :(得分: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();
    }
}