简体   繁体   中英

how to show received sms in textview instead of toast

I am currently working on a simple android application to read an SMS and print it in TexView instead of Toast. but in receiver activity, we do not initialise "findbyid" so we can't print the SMS in textView. now I am showing SMS in Toast to test but I do not want it in a toast. I am also read questions/answer and also other articles but can't find what I want.

receiver activity,

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

public class read_sms extends BroadcastReceiver {
// Get the object of SmsManager


@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    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();

                Log.i("SmsReciver", "senderNum: " + senderNum + ", message: " + message);
                //ourSMS.getSmsDetails(senderNum, message);
                // Show SMS notification
                //Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, Toast.LENGTH_LONG).show();


                if(message.equals("Milind")){
                    Toast.makeText(context, "sms matched", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(context, "not matched", Toast.LENGTH_LONG).show();
                }
            } // end of for loop
        } // bundle

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("SmsReciver", "Exception smsReciver" + e);
    }

}

}

my home activity is empty because I can't find which code placed here.

public class home extends AppCompatActivity {

TextView SMS_textview;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    SMS_textview = (TextView) findViewById(R.id.sms_text);


}
}

change that TextView to

public static TextView SMS_textview;

add method

public void recivedSms(String message)
{
    try
    {

     SMS_textview.setText(message);

    }
    catch (Exception e)
    {
    }
}

in read_sms class add the following code when you recieve sms

  home Sms = new home();
  Sms.recivedSms(message );

Register your Receiver in your activity whenever you Receive a text in receiver
use this code in your Receiver class

 Intent broadcastIntent = new Intent();
                broadcastIntent.putExtra("your key", your Value);
                broadcastIntent.setAction("link from  you have recieve a text");
                context.sendBroadcast(broadcastIntent);

after this register a broadcast Receiver in your activity like this

private void registerSmsReciever() {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("link from  you have recieve a text");

  BroadcastReceiver  broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("onReceive", "Sms recieved in progress");
          String  message= intent.getStringExtra("key");
            textView.setText(intent.getStringExtra("key"));


        }

    };

    registerReceiver(broadcastReceiver, intentFilter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM