This is a dialog question when playing Android YouTube.

Asked 2 years ago, Updated 2 years ago, 27 views

Private YouTubePlayer.PlaybackEventListener = new YouTubePlayer.PlaybackEventListener()
    {
        @Override
        public void onPlaying() {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            boolean isMobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
            Log.d("isMobile", "Mobile-Check" + isMobile+":");
            boolean isWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
            Log.d("isMobile", "Wifi-Check" + isWifi+":");
                if (isMobile && !isWifi) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Handdrip.this);
                    builder.setTitle ("Notification")
                            .setMessage ("Currently accessing 3g/4g". If it is not a Wi-Fi connection, you may be charged a data call fee. Would you like to go on?")
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                playbackEventListener.onPlaying();
                                }
                            })
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            });
                }
            }

So we've done this. First of all, I can check the internet, but my skills are still low Am I doing it wrong? No response when pressing play

android java

2022-09-22 20:44

2 Answers

AlertDialog.You must call the show() function after setting a series of values in Builder to expose the dialog to the screen. Add the show() function from the code you wrote as shown below.

...
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        finish();
    }
}).show();


2022-09-22 20:44

AlertDialog on current code.You can say that you set the options in the dialog window through Builder.

Now that I've set it up, I have to add the code below and show it to you.

There are two ways.

// 1. Create an AlertDialog object through builder.create and call the show() method

AlertDialog alertDialog = builder.create();
alertDialog.show();
// 2. Invoke the Show() method in the last method of builder, setNegativeButton()

.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            }).show();


2022-09-22 20:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.