美好的一天,在廣播接收機中,系統(tǒng)重新啟動后,我試圖顯示一個警報對話框。我已經(jīng)在清單中添加了接收者并調(diào)用了所需的權(quán)限,但是在顯示對話框時出現(xiàn)錯誤。請問我該如何正確實施呢?我的代碼: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 application01-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)
3 回答

蕪湖不蕪
TA貢獻(xiàn)1796條經(jīng)驗 獲得超7個贊
這是有關(guān)如何執(zhí)行此操作的帖子。您可以從此處獲取源代碼。
您不能直接從廣播接收器顯示對話框。您必須使用Activity
。另外,為了接收ACTION_BOOT_COMPLETED
您的活動,必須首先由用戶或其他應(yīng)用程序顯式啟動(有關(guān)更多信息,Google應(yīng)用程序處于停止?fàn)顟B(tài))。
基本上,要實現(xiàn)所需的功能,您需要執(zhí)行以下操作:
創(chuàng)建顯示對話框的透明活動。
創(chuàng)建
BroadcastReceiver
將接收ACTION_BOOT_COMPLETED
并開始您的活動。在清單中注冊您的廣播接收器并獲得適當(dāng)?shù)脑S可。
此外,此問題提供有關(guān)如何創(chuàng)建透明活動的更多信息。
- 3 回答
- 0 關(guān)注
- 491 瀏覽
添加回答
舉報
0/150
提交
取消