SOWMITHRA KUMAR G M
SOWMITHRA KUMAR G M

Reputation: 1530

How to insert TextView in Activity from other non activity class Android?

I need to set TextView object of Main3Activity (Activity class) from IncomingSms (Non activity class).

public class IncomingSms extends BroadcastReceiver {

    final SmsManager sms = SmsManager.getDefault();

    @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 senderNum = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();
                    if (message.equalsIgnoreCase("FIRE DETECTED")) {
                        //Problem start here: I cant able to set TextView object of Main3Activity
                        Main3Activity main3Activity = new Main3Activity();
                        TextView tv = (TextView) main3Activity.findViewById(R.id.firealert);
                        tv.setText(message);
                        //Problem end here
                        Log.i("SmsReceived", "senderNum: " + senderNum + "; message: " + message);
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(context, message, duration);
                        toast.show();
                    }
                }
            }
        } catch(Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);
        }
    }
}

Upvotes: 1

Views: 235

Answers (2)

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6823

create an interface class save it as java class then

public interface SmsListener {
            public void messageReceived(String messageText);
    }

and

final SmsManager sms = SmsManager.getDefault();
private static SmsListener mListener;
@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 senderNum = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();
                if (message.equalsIgnoreCase("FIRE DETECTED") ){
                    mListener.messageReceived(message); //add this
                    Log.i("SmsReceived", "senderNum: "+ senderNum + "; message: " + message);
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, message, duration);
                    toast.show();
                }
            }
        }
    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }
}
public static void bindListener(SmsListener listener) {
        mListener = listener;
    }

And in your Main3Activity onCreate method

TextView tv=(TextView) main3Activity.findViewById(R.id.firealert);


SmsReceiver.bindListener(new SmsListener() {
                @Override
                public void messageReceived(String messageText) {
                    Log.d("Text",messageText);
                     tv.setText(messageText);
                }
            });

Upvotes: 1

albeee
albeee

Reputation: 1472

You can't create the activity instance like that.

Solution: 1. Create a broadcast receiver in your activity and register for a custom intent. 2. Send a custom broadcast intent from your sms receiver. So basically that will reach activity receiver and then you can simply update the text there.

In case your activity is not started, simply create a activity intent and pass the sms text as part of the intent extras.

Upvotes: 1

Related Questions