Java 广播接收器内的AlertDialog??能做到吗?
广播接收器内的AlertDialog?能做到吗?我正在开发一个应用程序,如果我收到短信,它会弹出一个对话框。我正试图在BroadcaseReceiver中对其进行编码。但是我不能使用这行代码Java 广播接收器内的AlertDialog??能做到吗?,java,android,broadcastreceiver,android-alertdialog,Java,Android,Broadcastreceiver,Android Alertdialog,广播接收器内的AlertDialog?能做到吗?我正在开发一个应用程序,如果我收到短信,它会弹出一个对话框。我正试图在BroadcaseReceiver中对其进行编码。但是我不能使用这行代码AlertDialog.Builder=newalertdialog.Builder(这个)。谁能给我一个提示吗 public class SMSPopUpReceiver extends BroadcastReceiver { private static final String LOG_TAG
AlertDialog.Builder=newalertdialog.Builder(这个)代码>。谁能给我一个提示吗
public class SMSPopUpReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive");
if (intent.getAction().equals(SMSPopUpReceiver.ACTION)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (Object pdu : pdus){
SmsMessage messages =
SmsMessage.createFromPdu((byte[]) pdu);
sb.append("Received SMS\nFrom: ");
sb.append(messages.getDisplayOriginatingAddress());
sb.append("\n----Message----\n");
sb.append( messages.getDisplayMessageBody());
}
}
Log.i(SMSPopUpReceiver.LOG_TAG,
"[SMSApp] onReceiveIntent: " + sb);
Toast.makeText
(context, sb.toString(), Toast.LENGTH_LONG).show();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
}
}
将AlertDilaog中的单词“this”替换为“context”——onReceive方法的第一个参数
public void onReceive(Context context, Intent intent)
主要问题:尽量避免将耗时的功能放入BroadcastReceiver。它应该只接收并启动绑定活动/服务中的进一步处理
更新:
请检查以下可能有用的来源:
关于StackOverflow的类似问题:
Android SDK演示示例:
android sdk windows\samples\android-8\ApiDemos\src\com\example\android\API\os\SMSMessagedemo.java
当然还有标准的Android API文档:
更新2:
添加了应用程序框架,看起来应该是这样的。请注意,未定义任何内容视图。这是因为你的应用程序将有透明的屏幕。为了实现这一点
@android:style/Theme.半透明
在AndroidManifest.xml中此活动的主题标记下输入
public class NotifySMSReceived extends Activity
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION);
this.registerReceiver(mReceivedSMSReceiver, filter);
}
private void displayAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?").setCancelable(
false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action))
{
//your SMS processing code
displayAlert();
}
}
};
}
我一直在调查,实际上说:
公开摘要无效
(上下文、意图)
因为:API级别1此方法是
当广播接收器被激活时调用
接收有意图的广播。在期间
这次你可以用另一个
广播接收机的方法
查看/修改当前结果值。
该函数通常在内部调用
其进程的主线程,因此
永远不要执行长时间运行
其中的操作(有一个超时
系统允许的10秒时间
在考虑接收器是
被阻止,一名候选人将被杀)。
无法在中启动弹出对话框
您的onReceive()实现
无法在中启动弹出对话框
您的onReceive()实现
因此,似乎不可能您可以创建一个新的透明活动,然后在该活动中创建警报对话框,无论何时显示警报,都可以从广播接收器调用该活动,这可能会起作用,未经测试这很晚,但这可能会对某人有所帮助
您不能在广播接收器内使用警报对话框,我们只能在活动或服务中使用此对话框。像这样试试
在BroadcastReceive的onReceive方法中添加
Intent i = new Intent(context, yourclass.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
在您的类中设置对话框消息,以便在触发接收器事件时显示该消息。我试过这个,它对我有效。希望这可以帮助一些人:-)为什么你不能使用AlertDialog.Builder=newalertdialog.Builder(这个)代码>它对这个参考不满意吗?如果是这种情况,请尝试将其替换为调用getApplicationContext()
。我不确定这是否是你正在经历的问题。我觉得在这种情况下,在BroadcastReceiver返回之前,对话框只会在屏幕上闪烁一秒钟。无法使用getApplicationContext()!在我看来,自动弹出一个对话框(从用户的角度来看)听起来像是糟糕的UI设计。为什么不使用通知呢?这就是他们的目的。这是一个完整的例子,通过我尝试使用上下文的链接,但我从未看到DilaogIt从未出现,因为你必须show()
it<代码>AlertDialog alert=builder.create();alert.show()代码>我是如何获得此错误的:01-30 16:03:21.748:WARN/WindowManager(34):试图添加具有非应用程序令牌WindowToken{43c345e8 token=null}的窗口。中止。@Java Review:您是否已将代码更改为“AlertDialog.Builder=new AlertDialog.Builder(上下文);”正如乌梅什建议的那样?我认为你是对的。你能帮我解决这个问题吗!我建议您创建具有透明背景的服务或活动,并将BroadcastReceiver绑定到该服务或活动。查看一些androidsdk示例。另外,在我的个人资料中,我有一些关于这方面的有用问题/答案。你能告诉我去哪里找吗,我对这一切都是新手。googletalk me如果你想口吃Johnsmithu更新我的答案,包括有用的知识来源。zelimir我正在看代码,但我还是不明白。你能帮我离线吗