android - Onreceiver in broadcast not working in while receive the sms -


after incoming message in mobile number onreceive() not working.please me finding solution problem. code please check , correct me if wrong.

    public class incomingsms extends broadcastreceiver {         context con;         @override         public void onreceive(context context, intent intent) {             this.con = context;             if(intent.getaction().equals("android.provider.telephony.sms_received"))             {                 log.e("incomingsms", "onreceive..");                 final bundle bundle = intent.getextras();                 try {                     if (bundle != null) {                         (string key : bundle.keyset())                             log.e("okkk", key + "//   " + bundle.getstring(key));                          final object[] pdusobj = (object[]) bundle.get("pdus");                         (int = 0; < pdusobj.length; i++) {                             smsmessage currentmessage = smsmessage.createfrompdu((byte[]) pdusobj[i]);                             string phonenumber = currentmessage.getdisplayoriginatingaddress();                             string sendernum = phonenumber;                             string message = currentmessage.getdisplaymessagebody();                              try {                                 if (sendernum.equals("ta-docomo")) {                                     codeverificationactivity sms = new codeverificationactivity();                                     sms.recivedsms(message);                                 }                             } catch (exception e) {                             }                          }                     }                  } catch (exception e) {                     e.printstacktrace();                 }             }          }      }   

this manifest file code used.please tell me right way if missed in code correct me.actually doing reading incoming message automatically in android verify otp. important me. in advance

   <receiver android:name=".util.incomingsms"             android:enabled="true"             android:exported="true">             <intent-filter android:priority="999">                 <action android:name="android.provider.telephony.sms_received" />              </intent-filter>         </receiver> 

are checking on marshmallow. allow permission app setting.

or try this.

public class smsbroadcast extends broadcastreceiver {  final smsmanager manager = smsmanager.getdefault();  @override public void onreceive(context context, intent intent) {      final bundle bundle = intent.getextras();      try {         if (bundle != null) {             final object[] pdus = (object[]) bundle.get("pdus");              (int = 0; < pdus.length; i++) {                 smsmessage currentmessage = smsmessage.createfrompdu((byte[]) pdus[i]);                 string phonenumber = currentmessage.getdisplayoriginatingaddress();                  string message = currentmessage.getdisplaymessagebody();                  toast.maketext(context, "number " + phonenumber + " message :" + message, toast.length_short).show();               }           }       } catch (exception e) {         e.printstacktrace();      }  } } 

manifest.xml

    <uses-permission android:name="android.permission.receive_sms" /> <uses-permission android:name="android.permission.read_sms" /> <uses-permission android:name="android.permission.send_sms" />  <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:supportsrtl="true"     android:theme="@style/apptheme">      <receiver android:name="com.inficare.avinashverma.googlecloudmessagingdemo.broadcast.smsbroadcast">         <intent-filter>             <action android:name="android.provider.telephony.sms_received"></action>         </intent-filter>      </receiver>      <activity android:name=".mainactivity">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity> </application> 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -