How to track the messages in Android?

后端 未结 2 822
忘了有多久
忘了有多久 2020-12-29 16:26

I want to develop an app which tracks the sent/received SMSs. I mean, when a user sends a message from its device, the message detail should be saved to a table provided by

2条回答
  •  清歌不尽
    2020-12-29 16:53

    This is easy to do with a broadcast Receiver write in your Manifest:

    edit: seems only to work for SMS_RECEIVED see this thread

    
     
          
          
     
    
    

    And the Permission:

    
    

    Then Create the Receiver llike:

    public class SMSReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        //do something with the received sms        
           }else  if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
                //do something with the sended sms
         }
      }
    }
    

    To handle a incoming sms might look like:

    Bundle extras = intent.getExtras();
    Object[] pdus = (Object[]) extras.get("pdus");
    for (Object pdu : pdus) {
            SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu);
            String origin = msg.getOriginatingAddress();
            String body = msg.getMessageBody();
    ....
    }
    

    If you want to prevent a sms to be pushed in the commen InBox, you can achieve this with:

    abortBroadcast();
    

提交回复
热议问题