简体   繁体   English

将收到的短信显示为 textview android

[英]display incoming sms as textview android

i'm trying to develop app(tester) that display a incoming message as textview我正在尝试开发将传入消息显示为 textview 的应用程序(测试器)

i have incoming listener SMS class look like this (based on this code )我有传入的监听器短信 class 看起来像这样(基于此代码

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?我想将那里的“字符串消息”传递给我的主要活动……所以我可以将它显示为 texview……有机会吗? thank you谢谢你

You could implement your broadcast receiver directly from within the Activity that is supposed to display the message in a TextView.您可以直接从应该在 TextView 中显示消息的 Activity 中实现广播接收器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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