How to track the messages in Android?

后端 未结 2 809
忘了有多久
忘了有多久 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:49

    Please do not call abortBroadcast() you will prevent other apps receiving the SMS_RECEIVED ordered broadcast. This is bad behavior this is not what android is about. I dont understand why google even lets developer abort broadcasts of system intents like SMS.

    0 讨论(0)
  • 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

    <receiver android:name=".SMSReceiver"  android:enabled="true">
     <intent-filter android:priority="1000">
          <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
          <action android:name="android.provider.Telephony.SMS_SENT"/>
     </intent-filter>
    </receiver>
    

    And the Permission:

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    

    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();
    
    0 讨论(0)
提交回复
热议问题