0

I am getting the message from BroadcastReceiver but I am unable to update the EditText in my Activity. Message displayed in logcat using Log.i() but EditText is not updating.

my receiver class is as follows:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class IncomingSms extends BroadcastReceiver 
{
  @Override
  public void onReceive(Context context, Intent intent) 
  {

  final Bundle bundle = intent.getExtras();
  try {
  if (bundle != null) 
  {
   final Object[] pdusObj = (Object[]) bundle.get("pdus");
   for (int i = 0; i < 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")) 
     {
            Otp Sms = new Otp();
            Sms.recivedSms(message );
     }
  }
  catch(Exception e){}

  }
   }

  } catch (Exception e) 
  {

 }
 }

}

My Activity class

public class Otp extends Activity 
   {

       static EditText OtpNumber;
       protected void onCreate(Bundle savedInstanceState) 
         {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.otp);
                OtpNumber= (EditText) findViewById(R.id.txtName);
        }
      public void recivedSms(String message) 
        {
       try 
         {
          OtpNumber.setText(message);
         } 
         catch (Exception e) 
             {         
                   }
       }

       }

in mainfest file

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

    <receiver android:name=".IncomingSms">
        <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

i have followed this link

aga
  • 25,984
  • 9
  • 77
  • 115
austin
  • 37
  • 6
  • 2
    You can not create an instance of an activity class like `Otp Sms = new Otp();` . You should create a listener interface to callback this method `recivedSms`. Or the easiest way; do the method `recivedSms` static and call like that `Otp.receivedSms(message);` – Batuhan Coşkun Dec 18 '15 at 07:05
  • Thank you Batuhan i have changed the method to static and its working fine. – austin Dec 18 '15 at 07:19

1 Answers1

1

bro if your edit text is in another activity then you need to transfer the data (otp) to another activity using intent

here is the reference

Community
  • 1
  • 1
sKv
  • 66
  • 6