java - toast 线从未被调用

标签 java android toast listeners

当我在屏幕上按下图像时,它会像预期的那样调用 powerButton.OnClickListener() ,并且经过几秒钟的缓冲后,流播放得很好。然而,人们希望显示一个简短的 toast 弹出窗口来通知用户“ radio 流连接,请稍候...”

这就是问题所在,无论我尝试了什么以及将创建 toast 弹出窗口的行放置在何处,它在进入缓冲之前都不会显示。

任何帮助将不胜感激,我已经添加了大部分代码,并将在必要时提供更多代码。

        // run on powerButton click
    powerButton.setOnClickListener(new OnClickListener()
        {
        public void onClick(View view)
        {
            // check if the player is started or stopped
            if (isPlaying) // player is streaming
            {
                // stop the stream and set isPlaying to false
                mediaPlayer.stop();

                // release the media player
                releaseMediaPlayer();

                // update notification
                mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);

                // set power button to "powered off" image
                powerButton.setImageResource(R.drawable.power_off);
            }
            else // player not streaming
            {
                // notify the user that the stream is loading
                final Toast streamLoading = toast.makeText(getBaseContext(), "Radio Stream Connecting, Please Wait...", Toast.LENGTH_SHORT);
                streamLoading.show();

                // try catch block to attempt connecting to radioUrl
                try
                {                       
                    // create new instance of media player
                    mediaPlayer = new MediaPlayer();

                    // set media player to handle audio streams                    
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                    // connect to radio stream and fill buffer
                    mediaPlayer.setDataSource(radioUrl);
                    mediaPlayer.prepare(); // might take long depending on buffering speed

                    // start the media player and set isPlaying to true
                    mediaPlayer.start();
                    isPlaying = true;

                    // update notification, clear stream message
                    createNotification();

                    // set power button to "powered on" image
                    powerButton.setImageResource(R.drawable.power_on);
                }
                catch (IllegalArgumentException e) // cannot connect to stream
                {
                    // clear streaming text and notify user of failure
                    final Toast streamError1 = Toast.makeText(MainMenu.this, "Failed to load: Unable to connect to stream!", Toast.LENGTH_SHORT);
                    streamError1.show();

                    // release the media player and display error
                    releaseMediaPlayer();
                    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                    e.printStackTrace();
                }
                catch (IllegalStateException e) // stream cannot play audio
                {
                    // clear streaming text and notify user of failure
                    final Toast streamError2 = Toast.makeText(getBaseContext(), "Failed to load: cannot play stream!", Toast.LENGTH_SHORT);
                    streamError2.show();

                    // release the media player and display error
                    releaseMediaPlayer();
                    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                    e.printStackTrace();
                }
                catch (IOException e) // general connection issue
                {
                    // clear streaming text and notify user of failure
                    final Toast streamError3 = Toast.makeText(getBaseContext(), "Failed to Load: Connection issue!", Toast.LENGTH_SHORT);
                    streamError3.show();

                    // release the media player and display error
                    releaseMediaPlayer();
                    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
                    e.printStackTrace();
                }
            }

        }
    });

最佳答案

这是因为在您从 onClick 返回之前,Toast 不会显示,因为那是 UI 线程。在缓冲开始之前你不会这样做。我认为要获得您想要的效果,您可以检查 AsynchTask 在后台线程中进行缓冲并立即从 onClick 返回。

事实上,按照您的做法,如果缓冲时间太长,您的应用程序将被 Android 标记为无响应。

关于java - toast 线从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8364277/

相关文章:

c# - Toast 不显示

Java 在 servlet 上下文中存储与数据库查找

java - 应用程序服务器(java) : Should adding RAM to server depend on each domain's -Xmx value?

Java 正则表达式匹配 Java 代码中方法调用的第一个参数

java - 出现错误 java.lang.RuntimeException : Unable to start activity ComponentInfo

Android 事件计时(Toasts 和 Intents)

java - Google Sheet API V4 Java,从列号获取列字母

java - 如何去掉小数点后面的零

android - Spring 安卓: using RestTemplate with https and cookies

android - 从服务中的线程发送 toast