android - How to stop receiving same SMS multiple times? -


i have built following receiver class within messaging app, receives sms messages anytime:

public class receiver extends broadcastreceiver {     public void onreceive(context context, intent intent) {         bundle extras = intent.getextras();         smsmessage[] smgs = null;         string infosender = "";         string infosms = "";          if (extras != null) {             // retrieve sms message received             object[] pdus = (object[]) extras.get("pdus");             smgs = new smsmessage[pdus.length];              (int = 0; < smgs.length; i++) {                 smgs[i] = smsmessage.createfrompdu((byte[]) pdus[i]);                 infosender += smgs[i].getoriginatingaddress();                 infosms += smgs[i].getmessagebody().tostring();             }         }     } } 

the problem sometime receives same message multiple times, suspecting it's because receiver not unregistered , destroyed, checks incoming messages often.

hence, i've tried implementing typical receiver class:

public class myreceiver extends mainactivity {     broadcastreceiver receiverone = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          intentfilter filter = new intentfilter("android.provider.telephony.sms_received");         receiverone = new broadcastreceiver() {             @override             public void onreceive(context arr0, intent arr1) {                 processreceive(arr0, arr1);             }         };         registerreceiver(receiverone, filter);     }      protected void ondestroy(){         super.ondestroy();         unregisterreceiver(receiverone);     } } 

but now, receiverone never utilised, because there no layout class , hence it's never called. there way combine these 2 receivers?

or better still, have better suggestion why first receiver receiving sms messages multiple times?

here solution message , add stringbuilder make single why getting getoriginatingaddress again , again same sms parts.

public class receiver extends broadcastreceiver {         public void onreceive(context context, intent intent) {          if (intent.getaction().equals("android.provider.telephony.sms_received")) {             bundle extras = intent.getextras();             smsmessage[] smgs = null;             string infosender = "";             string infosms = "";              if (extras != null) {                 // retrieve sms message received                 object[] pdus = (object[]) extras.get("pdus");                 smgs = new smsmessage[pdus.length];                  stringbuilder sb = new stringbuilder();                 (int = 0; < smgs.length; i++) {                      smgs[i] = smsmessage.createfrompdu((byte[]) pdus[i]);                     sb.append(smgs[i].getmessagebody());                  }                 infosender = smgs[0].getoriginatingaddress();                 infosms = sb.tostring();             }          }        }     } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -