gpt4 book ai didi

java - 收到某些短信时如何在android上显示文本

转载 作者:太空狗 更新时间:2023-10-29 15:07:31 25 4
gpt4 key购买 nike

我是 android 开发和 java 编程的新手。我只是想知道如果收到此“特定”消息,如何打印或显示特定文本。 I got this code on the web on how to receive and read SMS .

ReadIncomingSms.java

package readnewsms.adk;

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

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the 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();



Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();


}
// end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}
}

然后我在 toast.show() 之后添加了这段代码;仍在循环中

if  (message.equals(("1"))){
System.out.println("user sends 1");
} else {
System.out.println("other");
}

我该怎么做才能在屏幕上显示这些消息?或者如果消息包含特定值,我如何评估代码,然后我将根据发送的消息打印特定消息。例如,如果 INCOMING 消息包含 1,则应用程序将显示用户发送 1。

最佳答案

看起来您想在屏幕上放置一个Toast。这会在底部显示一条小消息。代码如下所示:

if  (message.equals(("1"))){
Toast toast = Toast.makeText(context, "User sent 1");
toast.show();
} else {
Toast toast = Toast.makeText(context, "Other);
toast.show();
}

关于java - 收到某些短信时如何在android上显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com