skip to Main Content

I am new user of android studio. I am using priority in manifest file and all required permissions but I do not know how can I use result in main activity, help me out.

public abstract class SmsBroadcastReceiver extends BroadcastReceiver {
   
    protected abstract void onSmsReceived(SmsMessage smsMessage);```

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if (bundle != null) {
            Object[] pduObjectList = (Object[]) bundle.get("pdus");
            if (pduObjectList != null) {
                for (Object pduObject : pduObjectList) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pduObject);
                    onSmsReceived(smsMessage);
                }
            }
        }
    }
}

3

Answers


  1. One easy way is by using static variables.

    On the MainActivity add the following:

    public class MainActivity extends AppCompatActivity{
        public static SmsMessage result;
        //...
    }
    

    Then, add the following line MainActivity.result = smsMessage to the code you provided just after you get the message.

    Then you can use the variable result in any part of the MainActivity (also by calling MainActivity.result you have the value of the variable in any part of your code).

    Just be careful, static variables are defined as null before any assignment.

    Login or Signup to reply.
  2. Automatic SMS Verification with the SMS Retriever API used so this is easily getting in SMS,So there was no problem and No permission is needed

    https://developers.google.com/identity/sms-retriever/overview

    Login or Signup to reply.
  3. The abstract keyword is a non-access modifier, used for classes and methods:

    Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

    Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

    In your activity:

    public class MyActivity extends Activity {
    
        private smsReceiver SmsBroadcastReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_one_position);
            smsReceiver = new SmsBroadcastReceiver() {
    
                // this code is call asyncrously from the receiver
                @Override
                protected void onSmsReceived() {
                     // Add your activty logic here
                }
    
            };
    
        }
    
         @Override
            protected void onPause() {
                super.onPause();
                this.unregisterReceiver(this.smsReceiver);
            }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search