show an alert dialog in broadcast receiver after a system reboot
美好的一天,我试图在广播接收器中重新启动系统后显示一个警告对话框。 我在清单中添加了接收器并调用了所需的权限,但在显示对话框时出错。 请问我怎样才能正确实现这一点?...谢谢
我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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(); } |
我收到此日志错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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) |
问题是你试图从
您应该执行其他操作,在启动时启动
这是一篇更多关于此的博客文章。
编辑:
以下是我推荐的方法。从你的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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(); } } } } |
如你所见,我从不打电话给
你不能在BroadcastReceiver上使用对话框,
所以你最好从BroadcastReceiver调用对话框的活动,
在onReceive函数中添加此代码:
1 2 3 4 5 6 7 | @Override public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, {CLASSNAME}.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } |
用对话框活动填充{CLASSNAME},继承我的对话活动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.example.mbanking; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; // ALERT DIALOG // Sources : http://techblogon.com/alert-dialog-with-edittext-in-android-example-with-source-code/ public class AlertDialogActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder .setTitle("Test") .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(); } } |
我得到答案的地方?,这里:你如何在android的广播接收器中使用警告对话框?
感谢Femi !!,我只传播新闻:D
这是一篇关于如何做的帖子。您可以从这里获取源代码。
您无法直接从广播接收器显示对话框。你必须使用
基本上,要实现所需的功能,您需要:
此外,此问题提供了有关如何创建透明活动的更多信息。
最好的方法是创建一个活动并将其"Theme"属性设置为"Theme.Translucen"
1 2 3 4 5 6 | <activity android:name=".MyAlertDialog" android:label="@string/title_activity_alert_dialog" android:launchMode="singleInstance" android:theme="@android:style/Theme.Translucent"> </activity> |
并在您的活动中创建一个警告对话框:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class MyAlertDialog extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //hide activity title setContentView(R.layout.activity_my_alert_dialog); AlertDialog.Builder Builder=new AlertDialog.Builder(this) .setMessage("Do You Want continue ?") .setTitle("exit") .setIcon(android.R.drawable.ic_dialog_alert) .setNegativeButton(R.string.No, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MyAlertDialog.this.finish(); } }) .setPositiveButton(R.string.Yes,null); AlertDialog alertDialog=Builder.create(); alertDialog.show(); } } |
在brodcastreciver中:
1 2 3 4 5 6 7 8 9 | @Override public void onReceive(Context context, Intent intent) { Intent i=new Intent(context.getApplicationContext(),MyAlertDialog.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } |
如果我们使用此标志"FLAG_ACTIVITY_NEW_TASK"启动活动,则无法从堆栈中删除。
因此在关闭应用程序之后,我们尝试从堆栈启动应用程序,它显示相同的Activity,因为此活动具有标志"FLAG_ACTIVITY_NEW_TASK",因此它不应创建新实例并使用现有实例。
但我们只想展示一次对话框。为此,我们需要处理程序化的盟友。
1 2 3 4 5 6 7 8 9 10 11 12 13 | if (count == 0) { mBuilder = new Dialog(this); mMsg = getIntent().getStringExtra(AlarmSchedulerUtils.EXTRA_MSG); Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); mRingTome = RingtoneManager.getRingtone(ReminderDialog.this, notification); mRingTome.play(); count++; showReminderDialog(); } else { Intent intent=new Intent(ReminderDialog.this,SplashActivity.class); startActivity(intent); finish(); } |
这对我有用。
这可能是旧的和回答的线程,但回答的答案根本没有帮助。
You cannot launch a popup dialog in your implementation of onReceive().
BroadcastReceiver
您可以使用以对话框为主题的活动,而不是对话框或popupWindow
1 2 3 4 5 | <activity android:taskAffinity="" android:name=".activity.CallActivity" android:label="@string/app_name" android:theme="@style/AppTheme.Dialog" /> |
请注意我在块中添加taskAffinity(AndroidManifest.xml)
然后你可以将它用作常规活动。
1 2 3 4 | Intent intentPhoneCall = new Intent(context, CallActivity.class); intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intentPhoneCall); |
希望能帮助到你。快乐的编码。