smsRecevicer 类中的警报问题
我是新来的。我正在制作一个应用程序,它必须在短信上显示警报。 我想我不明白我是如何分手的。当函数具有“this”属性时,可以从另一个类调用该函数。
但这条线不起作用:
AlertDialog.Builder(this).setTitle("asd").setMessa ge(str).setNegativeButton("Annuller", null).setPositiveButton("Bekræft", null).show();
我不确定是因为这个类在后台运行还是什么?
这是班级:
package net.sms;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
AlertDialog.Builder alt_bld = new AlertDialog.Builder(context);
alt_bld.setTitle("aaa");
alt_bld.show();
//new AlertDialog.Builder(this).setTitle("asd").setMessage(str).setNegativeButton("Annuller", null).setPositiveButton("Bekræft", null).show();
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
I am new here. I are making a app, where it must show a alert on sms message.
I don't think I understand how i ex. can call a function from another class, when the function have a "this"-attribute.
But this line do not work:
AlertDialog.Builder(this).setTitle("asd").setMessa ge(str).setNegativeButton("Annuller", null).setPositiveButton("Bekræft", null).show();
I am not sure if it is because of this class run i background or what?
This is the class:
package net.sms;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
AlertDialog.Builder alt_bld = new AlertDialog.Builder(context);
alt_bld.setTitle("aaa");
alt_bld.show();
//new AlertDialog.Builder(this).setTitle("asd").setMessage(str).setNegativeButton("Annuller", null).setPositiveButton("Bekræft", null).show();
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您无法在广播接收器中启动警报。您可以通过启动一个看起来像警报的活动来达到类似的效果。
注意:Google 不建议从后台服务或接收器启动活动。
Manifest.xmlalert_activity.xmlSmsReceiver.java
I don't think you could start an alert in a Broadcast Receiver. You could achieve a similar affect by starting an activity that looks like an alert.
Note: Google does not recommend starting activities from background services or receivers.
Manifest.xml
alert_activity.xml
SmsReceiver.java
接收者不喜欢警报对话框。您可以使用通知或 Toast。
Receivers don't like alert dialogs. You can use a notification or toast.