你好,我想表现出一个系统重新启动广播接收机中的后一个警告对话框。我已经添加了接收器在我的清单,并要求所需的权限,但我得到一个错误显示的对话框。请我如何正确地实现这一点?..谢谢
我的code:
公共无效的onReceive(最终上下文的背景下,意图意图){
Log.d(TAG,收到启动完成广播接收器...开始设置);
。字符串设置= context.getResources()的getString(R.string.restart_setting);
String是= context.getResources()的getString(R.string.Settings)。
字符串否= context.getResources()的getString(R.string.Cancel)。
最后AlertDialog.Builder建设者=新AlertDialog.Builder(上下文);
builder.setMessage(设置)
.setCancelable(假)
.setPositiveButton(是的,新DialogInterface.OnClickListener(){
公共无效的onClick(@燮pressWarnings(未使用)最终DialogInterface对话框,@燮pressWarnings(未使用)最终诠释ID)
配置意图=新的意图(背景下,WeatherConfigure.class)
context.startActivity(配置);
}
})
.setNegativeButton(不,新DialogInterface.OnClickListener(){
公共无效的onClick(最终DialogInterface对话框,@燮pressWarnings(未使用)最终诠释ID){
dialog.cancel();
}
});
最后AlertDialog警报= builder.create();
alert.show();
}
我得到这个日志错误:
一月一日至七日:42:01.559:ERROR / AndroidRuntime(2004年):android.view.WindowManager $ BadTokenException:产生的原因无法添加窗口 - 令牌null不是一个应用程序
1月1号至7日:42:01.559:ERROR / AndroidRuntime(2004年):在android.view.ViewRoot.setView(ViewRoot.java:548)
1月1号至7日:42:01.559:ERROR / AndroidRuntime(2004年):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
1月1号至7日:42:01.559:ERROR / AndroidRuntime(2004年):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
1月1号至7日:42:01.559:ERROR / AndroidRuntime(2004年):在android.app.Dialog.show(Dialog.java:288)
1月1号至7日:42:01.559:ERROR / AndroidRuntime(2004年):在com.MuaaApps.MyWeatherUpdate.myWeatherBroadcastReceiver.onReceive(MyWeatherBroadcastReceiver.java:59)
1月1号至7日:42:01.559:ERROR / AndroidRuntime(2004):在android.app.ActivityThread.handleReceiver(ActivityThread.java:1994)
解决方案
现在的问题是,你正在试图表明一个 AlertDialog
从的BroadcastReceiver
,这是不允许的。你不能表现出 AlertDialog
从的BroadcastReceiver
。只有活动可以显示的对话框。
您应该做些别的事情,有的BroadcastReceiver
启动时自动打开为你做的,开始活动,以显示该对话框。
下面是一个博客文章更多相关信息。
编辑:
下面是我会建议这样做。从你的的BroadcastReceiver
启动活动
与 AlertDialog
这样..
公共类NotifySMSReceived扩展活动
{
私有静态最后弦乐LOG_TAG =SMSReceiver;
公共静态最终诠释NOTIFICATION_ID_RECEIVED = 0x1221;
静态最后弦乐ACTION =android.provider.Telephony.SMS_RECEIVED;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
IntentFilter的过滤器=新的IntentFilter(ACTION);
this.registerReceiver(mReceivedSMSReceiver,过滤器);
}
私人无效displayAlert()
{
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setMessage(你确定要退出吗?)。setCancelable(
假).setPositiveButton(是,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
})。setNegativeButton(否,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
});
AlertDialog警报= builder.create();
alert.show();
}
私人最终的BroadcastReceiver mReceivedSMSReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
串动= intent.getAction();
如果(ACTION.equals(动作))
{
//您的短信处理code
displayAlert();
}
}
}
}
正如你在这里看到我从来没有所谓的的setContentView()
。这是因为活动将有一个透明的视图,只有所述警报对话框显示。
Good day, I am trying to show an alert dialog after a system reboot in a broadcast receiver. I have added the receiver in my manifest and called the required permission, but am getting an error in showing the dialog. Please How can i implement this correctly?.. Thank you
my code:
public void onReceive(final Context context, Intent intent) {
Log.d(TAG, "received boot completed broadcast receiver... starting settings");
String settings = context.getResources().getString(R.string.restart_setting);
String yes = context.getResources().getString(R.string.Settings);
String no = context.getResources().getString(R.string.Cancel);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(settings)
.setCancelable(false)
.setPositiveButton(yes, new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id)
Intent config = new Intent(context, WeatherConfigure.class)
context.startActivity(config);
}
})
.setNegativeButton(no, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
am getting this log error:
01-07 01:42:01.559: ERROR/AndroidRuntime(2004): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-07 01:42:01.559: ERROR/AndroidRuntime(2004): at android.view.ViewRoot.setView(ViewRoot.java:548)
01-07 01:42:01.559: ERROR/AndroidRuntime(2004):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-07 01:42:01.559: ERROR/AndroidRuntime(2004): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-07 01:42:01.559: ERROR/AndroidRuntime(2004):at android.app.Dialog.show(Dialog.java:288)
01-07 01:42:01.559: ERROR/AndroidRuntime(2004):at com.MuaaApps.MyWeatherUpdate.myWeatherBroadcastReceiver.onReceive(MyWeatherBroadcastReceiver.java:59)
01-07 01:42:01.559: ERROR/AndroidRuntime(2004): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1994)
解决方案
The problem is you are trying to show an AlertDialog
from a BroadcastReceiver
, which isn't allowed. You can't show an AlertDialog
from a BroadcastReceiver
. Only activities can display dialogs.
You should do something else, have the BroadcastReceiver
start on boot as you do and start an activity to show the dialog.
Here is a blog post more on this.
EDIT:
Here is how I would recommend doing it. From your BroadcastReceiver
start an Activity
with an AlertDialog
as such..
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();
}
}
}
}
As you see here I NEVER called setContentView()
. This is because the activity will have a transparent view and only the alert dialog will show.