gpt4 book ai didi

android - 系统重新启动后在广播接收器中显示警报对话框

转载 作者:IT老高 更新时间:2023-10-28 21:47:48 27 4
gpt4 key购买 nike

您好,我正在尝试在广播接收器中显示系统重新启动后的警报对话框。我已在 list 中添加了接收者并调用了所需的权限,但在显示对话框时出现错误。请问我该如何正确实现?..谢谢

我的代码:

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();

}

我收到此日志错误:

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)

最佳答案

问题是您试图显示来自 BroadcastReceiverAlertDialog,这是不允许的。您不能显示来自 BroadcastReceiverAlertDialog。只有 Activity 才能显示对话框。

您应该做其他事情,让 BroadcastReceiver 像您一样在启动时启动,并启动一个 Activity 来显示对话框。

这里是 blog post更多信息。

编辑:

这是我推荐的做法。从你的 BroadcastReceiver 开始一个 Activity 和一个 AlertDialog 像这样..

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();
}
}
}
}

正如您在此处看到的,我从未调用过 setContentView()。这是因为 Activity 将具有透明 View ,并且只会显示警报对话框。

关于android - 系统重新启动后在广播接收器中显示警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com