我在我的应用中使用了AlarmManager。我想在报警发生时向用户显示一个警告。我使用了一个AlertDialog,但它给出了一个错误。我该如何解决这个问题?我想把警报声和振动。任何链接或代码。
public class AReceiver extends BroadcastReceiver{
AlertDialog alertDialog;
public void onReceive(Context context, Intent intent) {
alertDialog = new AlertDialog.Builder(this).create(); // Error here: The constructor AlertDialog.Builder is undefined.
alertDialog.setTitle("title");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
}
}
发布于 2011-07-06 20:53:11
您好,您不能在BroadcastReceiver
中使用AlertDialog。
您可以在BroadcastReciver
中调用另一个Activity类,如下所示。
Intent myIntent = new Intent(context, AlarmActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
在这个类中,您使用了Alert Dialog。
发布于 2016-10-07 23:02:00
我可以向你展示如何解决这个问题的主要思想。
应在非静态上下文而不是静态上下文中执行
对于这个问题,我有两个解决方案。
因此,当您在非静态上下文中获取警报事件时,您可以使用AlertDialog。
发布于 2016-10-28 03:37:52
虽然晚了,但可能对某些人还是有用的:
更正代码,如下所示:
alertDialog = new AlertDialog.Builder(context).create(); // Now The constructor AlertDialog.Builder is defined.
https://stackoverflow.com/questions/6596586
复制相似问题