Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通知类似于Toast,但持续时间较长_Java_Android_Notifications_Toast_Duration - Fatal编程技术网

Java 通知类似于Toast,但持续时间较长

Java 通知类似于Toast,但持续时间较长,java,android,notifications,toast,duration,Java,Android,Notifications,Toast,Duration,我必须创建一个类似android toast通知的通知,但我需要从服务中抛出它,并在需要时关闭它。 标准的吐司通知会很完美,但它太短了 我尝试使用DialogFragment,但它需要焦点(不像toast),我无法从服务中抛出它,只能从一个FragmentActivity中抛出 谢谢 在SO中快速搜索,您可以找到烤面包: 现在,您可以一个接一个地显示多个祝酒词,使其看起来持续时间更长: final String msg = "Some text"; Runnable delayedToast =

我必须创建一个类似android toast通知的通知,但我需要从服务中抛出它,并在需要时关闭它。 标准的吐司通知会很完美,但它太短了

我尝试使用DialogFragment,但它需要焦点(不像toast),我无法从服务中抛出它,只能从一个FragmentActivity中抛出


谢谢

在SO中快速搜索,您可以找到烤面包:

现在,您可以一个接一个地显示多个祝酒词,使其看起来持续时间更长:

final String msg = "Some text";
Runnable delayedToast = new Runnable() {
    @Override
    public void run() {
        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
    }
};

Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show(); 
mHandler.postDelayed(delayedToast, 3000);
mHandler.postDelayed(delayedToast, 6000);

其中,
ctx
是您的活动/应用程序上下文,
mHandler
是UI线程上的处理程序。持续时间应该在3000+3000+3500左右。

通过扩展toast类创建自定义toast并在其中设置持续时间。您看到这个问题了吗?克劳顿推荐的图书馆看起来符合你的标准。请查看下面的链接。。
            Toast toast = new Toast(this);
            TextView textView=new TextView(this);
            textView.setTextColor(Color.BLUE);
            textView.setBackgroundColor(Color.TRANSPARENT);
            textView.setTextSize(20);
            textView.setText("My Toast For Long Time");
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

            toast.setView(textView);

          timer =new CountDownTimer(20000, 1000)
            {
                public void onTick(long millisUntilFinished)
                {
                    toast.show();
                }
                public void onFinish()
                {
                    toast.cancel();
                }

            }.start();
            Toast toast = new Toast(this);
            TextView textView=new TextView(this);
            textView.setTextColor(Color.BLUE);
            textView.setBackgroundColor(Color.TRANSPARENT);
            textView.setTextSize(20);
            textView.setText("My Toast For Long Time");
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

            toast.setView(textView);

          timer =new CountDownTimer(20000, 1000)
            {
                public void onTick(long millisUntilFinished)
                {
                    toast.show();
                }
                public void onFinish()
                {
                    toast.cancel();
                }

            }.start();