Java 单击通知后未显示AlertDialog
我正在做以下工作: 我的应用程序有一个AsyncTask,它最终会创建一个AlertDialog来请求用户输入代码。当应用程序位于前台时,可能会发生这种情况,因此我会启动一个通知来通知用户,我的想法是,一旦用户单击通知,就会显示AlertDialog的主要活动 然而,一旦用户单击通知,就会显示AlertDialog应该显示的活动,我得到一个WindowLeaked异常,可能是由该对话框引起的 下面是我用来启动通知的代码(来自AsyncTask的onProgressUpdate方法): 任何想法都欢迎。Java 单击通知后未显示AlertDialog,java,android,android-asynctask,notifications,android-alertdialog,Java,Android,Android Asynctask,Notifications,Android Alertdialog,我正在做以下工作: 我的应用程序有一个AsyncTask,它最终会创建一个AlertDialog来请求用户输入代码。当应用程序位于前台时,可能会发生这种情况,因此我会启动一个通知来通知用户,我的想法是,一旦用户单击通知,就会显示AlertDialog的主要活动 然而,一旦用户单击通知,就会显示AlertDialog应该显示的活动,我得到一个WindowLeaked异常,可能是由该对话框引起的 下面是我用来启动通知的代码(来自AsyncTask的onProgressUpdate方法): 任何想法都
非常感谢:)最好创建一个单独的活动(例如DialogActivity)并在清单集中创建DialogActivity。您的通知创建似乎很好,您从onProgressUpdate调用该方法也很好。
因此,您的活动及其处理对话框的方式一定有问题 如果您可以提供您的主类,我们可以肯定地知道,但我猜在您的AsyncTask中,您会显示某种对话框,可能是一个进度对话框。
如果是这种情况,那么当您收到通知(在进度更新时)并单击它时,您将再次启动主活动,因此您将创建主活动的另一个实例,从而创建泄漏窗口。 尝试添加标志。
如API所述: 如果已设置,并且正在启动的活动已在当前任务中运行,则不会启动该活动的新实例,而是关闭其上的所有其他活动,并且此意图将作为新意图传递给(现在位于顶部的)旧活动 希望这能帮到你
public void launch_notification(){
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.opening_request_notification_title))
.setContentText(context.getString(R.string.opening_request_notification_text));
/* The intent must be created with an specific content and then frozen to be used
later using a pending intent.
*/
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}