调用AlertDialog的dismiss方法没有发生任何事情

Posted

技术标签:

【中文标题】调用AlertDialog的dismiss方法没有发生任何事情【英文标题】:Nothing happening on calling dismiss method of AlertDialog 【发布时间】:2021-10-12 04:36:56 【问题描述】:

基本上我有 2 个类,“ShowAlert”和“CallReceiver”,警报创建没有任何问题,但是当我尝试调用方法“alertDisplay.dismissAlert();”时它只是没有发生任何事情,警报仍然存在并且控制台没有显示任何错误,在互联网上搜索了几天不是答案,我在互联网上没有找到任何有用的东西来解决我的问题

这些是课程,感谢您的帮助

CallReceiver.java

package com.numbertracker;

import android.app.KeyguardManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

//BroadcastReceiver serve ad avere tutte quelle funzioni per controllare chiamate e altro in arrivo
public class CallReceiver extends BroadcastReceiver 

     ShowAlert alertDisplay = new ShowAlert();
    //onReceive serve a prendere le informazioni delle chiamate a seconda dello stato in cui si trovamo, di seguito piu info
    @Override
    public void onReceive(final Context context, Intent intent)
    

        try 
            //state è una variabile che serve a sapere appunto in che stato è il telefono, bloccato? sta squillando? sei in chiamata? ecc.
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            StringBuilder sb = new StringBuilder(); //serve a trasformare la stringa number in un Text visto che Toast non vuole String
            EDITED, NOW AS A PRIVATE VARIABLE ShowAlert alertDisplay = new ShowAlert();
            KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);

            if(!intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER))
                return;
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
                //Log.e("Zed", "ringing");
                String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                sb.append(number);
                number = sb.toString();

                if( myKM.inKeyguardRestrictedInputMode()) 
                    showNotification(context,"bla bla",intent);
                 else 
                    alertDisplay.display("bla bla",context);
                

                //Toast.makeText(context,number, Toast.LENGTH_SHORT).show();
            
            if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
                alertDisplay.dismissAlert();
                //Log.e("Zed", "offhook");
                //Toast.makeText(context, "Didn't answer the call", Toast.LENGTH_SHORT).show();
            
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) 
                alertDisplay.dismissAlert();
                //Log.e("Zed", "idle");
                //Toast.makeText(context, "Call in hold state", Toast.LENGTH_SHORT).show();
            

        
        catch (Exception e)
            e.printStackTrace();
        
    

    public void showNotification(Context context, String title, String body, Intent intent) 
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        int notificationId = 1;
        String channelId = "channel-01";
        String channelName = "LockScreen";
        int importance = NotificationManager.IMPORTANCE_HIGH;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) 
            NotificationChannel mChannel = new NotificationChannel(
                    channelId, channelName, importance);
            notificationManager.createNotificationChannel(mChannel);
        

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        mBuilder.setContentIntent(resultPendingIntent);

        notificationManager.notify(notificationId, mBuilder.build());
    


ShowAlert.java

package com.numbertracker;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.WindowManager;
import android.widget.Toast;


public class ShowAlert 

    //funzione che puo essere richiamata quando si vuole per mostrare un alert, ho creato una classe apposita in caso
    //si volessero aggiungere piu alert senza intasare main o altre funzioni, cosi puo essere chiamata ovunque

    private AlertDialog alert;

    public void display(String title, Context context)
    
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("NumberTracker Alert")
                .setMessage(title)
                .setCancelable(false)
                //.setPositiveButton("Block Number", (dialog, which) -> Toast.makeText(context, "Selected Option: YES", Toast.LENGTH_SHORT).show())
                .setNegativeButton("Dismiss", new DialogInterface.OnClickListener()  // define the 'Cancel' button
                    public void onClick(DialogInterface dialog, int which) 
                        dialog.cancel();
                    
                );
        alert = builder.create();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            alert.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
         else 
            alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        
        alert.show();

    

    public void dismissAlert()
        if(alert != null)
            alert.dismiss();
        
    




清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.numbertracker">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.NumberTracker">

        <receiver android:name=".CallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

</manifest>

【问题讨论】:

ShowAlert alertDisplay = new ShowAlert(); – 每次运行onReceive(),您都会创建一个ShowAlert 的新实例,因此您调用display("bla bla",context) 的实例与您的实例不同打电话给dismissAlert()。那个从来没有调用过display(),所以当你调用它的dismissAlert()时它的alert仍然是空的。 天哪,我太傻了,反正我试着把 ShowAlert alertDisplay = new ShowAlert();作为类的私有变量,但它仍然不起作用 我想我从来不知道这一点,但是像这样设置窗口类型,在必要的权限下,允许从静态接收器显示Dialogs。有趣的。 Anyhoo,对不起,无视我之前的评论。如果您确实像我现在认为的那样进行了此设置 - 即,通过在清单中静态注册 CallReceiver&lt;receiver&gt; 元素 - 那么问题仍然是同一件事:ShowAlert 的多个实例。清单中注册的接收器类为每次广播重新实例化,因此为RINGING 运行的实例与为OFFHOOK 运行的实例不同。 对不起,我也忘了放我的清单,我在主要问题上添加了它,我已经有接收器了 是的,我想。我是说,该设置仍然存在相同的基本问题,因为您最终会得到多个 ShowAlert 实例,因为每次广播都会创建一个新的 CallReceiver 实例;例如,RINGING 一个实例,OFFHOOK 一个单独实例。 【参考方案1】:

不要打电话给alert.dismiss()。致电builder.dismiss()。 因为AlertDialog.Builder 有命令dismiss(),而不是alert

正确的代码:

builder.dismiss(),并在display()函数之外的类中定义AlertDialog.Builder builder,以便dismissAlert()函数也可以使用它来关闭Dialog。

【讨论】:

对不起,但这不是警报在 android 中的工作方式,您创建构建器并设置所需的所有属性,然后设置 AlertDialog abc = builder.build();直到现在警报还不存在... 试试这个代码,这对我有用。此外,在 onCreate 方法中,它被初始化。那么有什么问题呢?

以上是关于调用AlertDialog的dismiss方法没有发生任何事情的主要内容,如果未能解决你的问题,请参考以下文章

android AlertDialog.Builder.setView(v) 点击v里面一个按钮怎么让对话框消失。这个问题你怎么解决的

Android开发——diglog cancel与dismiss方法区别

如何关闭 AlertDialog.Builder?

调用dismiss方法后如何刷新ViewController

android 怎么设置alertdialog按返回键退出,点其他地方不退出

在 View Dismissal 之前调用 Delegate 方法