[英]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. 我目前正在开发一个简单的android应用程序,以读取SMS并在TexView中而不是Toast中进行打印。 but in receiver activity, we do not initialise "findbyid" so we can't print the SMS in textView.
但是在接收方活动中,我们不会初始化“ findbyid”,因此我们无法在textView中打印SMS。 now I am showing SMS in Toast to test but I do not want it in a toast.
现在,我正在Toast中显示SMS进行测试,但我不希望在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 将该TextView更改为
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 在收到短信后,在read_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 在您的Receiver类中使用此代码
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.