简体   繁体   中英

display incoming sms as textview android

i'm trying to develop app(tester) that display a incoming message as textview

i have incoming listener SMS class look like this (based on this code )

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;
import android.widget.Toast;

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();


@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);
                


            } // end of for loop
        } // bundle

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

i want to pass the "String message" in there to my mainactivity....so i can dispaly it as texview...any chance to do it? thank you

You could implement your broadcast receiver directly from within the Activity that is supposed to display the message in a TextView.

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