Android的吐司开始从服务只显示一次 [英] Android Toast started from Service only displays once
问题描述
我有一个监控套接字连接的服务。当连接丢失,需要显示敬酒通知它重新连接用户。这工作正常的第一次。从那以后,我看到了enqueueToast在日志中,但不显示敬酒。任何想法是AP preciated。我认为这将是一件容易的事情增加,但我必须失去了一些东西。
日志条目
信息/ NotificationService(118):enqueueToast PKG = com.abc callback=android.app.ITransientNotification$Stub$Proxy@43f7b100 持续时间= 1
code调用吐司
公共类ConnectionService扩展服务
{.....
公共无效restartConnection()
{
尝试
{
Log.i(this.toString(),尝试重新连接......);
//增加等待之间的每个重试,直到最大达到
INT睡眠时间= reconnectCounter * MIN_RECON_WAIT;
如果(睡眠时间与GT; MAX_RECON_WAIT)
{
睡眠时间= MAX_RECON_WAIT;
}
弦乐味精=的连接已丢失,重新启动尝试将开始:+睡眠时间/ 1000 +秒;
Log.i(this.toString(),味精);
Toast.makeText(getApplicationContext(),味精,Toast.LENGTH_LONG).show();
视频下载(睡眠时间);
//增加计数器
reconnectCounter ++;
this.startConnectionThread();
}
赶上(例外五)
{
Log.e(this.toString(),异常:+ e.toString());
e.printStackTrace();
}
} //结束retartConnection
是的,你可以用runOnUiThread去,这是一个合法的方式。
此外,您可以尝试的处理器选择。无论哪种方式,它应该工作。
下面是从我头顶一些code。我没有SDK,现在来测试它,但我认为它应该给你一个总体思路。
公共类ConnectionService延伸服务{
私人处理程序处理程序=新的处理程序();
公共无效restartConnection(){
INT睡眠时间= reconnectCounter * MIN_RECON_WAIT;
如果(睡眠时间与GT; MAX_RECON_WAIT)
{
睡眠时间= MAX_RECON_WAIT;
}
弦乐味精=的连接已丢失,重新启动尝试将开始:+睡眠时间/ 1000 +秒;
(新计时器())。时间表(
新的TimerTask(){
公共无效的run(){
handler.post(新的Runnable(){
公共无效的run(){
Toast.makeText(getApplicationContext(),msg中,Toast.LENGTH_LONG).show();
reconnectCounter ++;
this.startConnectionThread()
}
});
}
}, 睡觉时间);
} //结束restartConnection
} //结束ConnectionService
I have a service that monitors a socket connection. When the connection is lost it needs to display a Toast informing the user that it is reconnecting. This works fine the first time. After that I see the enqueueToast in the log but the toast is not displayed. Any ideas are appreciated. I thought this was going to be an easy thing to add, but I must be missing something.
Log entry
INFO/NotificationService(118): enqueueToast pkg=com.abc callback=android.app.ITransientNotification$Stub$Proxy@43f7b100 duration=1
Code that calls the Toast
public class ConnectionService extends Service
{ .....
public void restartConnection()
{
try
{
Log.i(this.toString(), "Attempting to reconnect...");
// increase the wait between each retry until the max is reached
int sleepTime = reconnectCounter * MIN_RECON_WAIT;
if (sleepTime > MAX_RECON_WAIT)
{
sleepTime = MAX_RECON_WAIT;
}
String msg = "The connection has been lost. Restart attempt will start in: " + sleepTime/1000 + " seconds";
Log.i(this.toString(), msg);
Toast.makeText(getApplicationContext(), msg , Toast.LENGTH_LONG).show();
Thread.sleep(sleepTime);
// increment the counter
reconnectCounter++;
this.startConnectionThread();
}
catch (Exception e)
{
Log.e(this.toString(), "Exception: " + e.toString());
e.printStackTrace();
}
}// end retartConnection
Yeah, you could go with the runOnUiThread, that's a legit way.
Also, you could try the Handler alternative. Either way it should work.
Here is some code from the top of my head. I don't have the SDK now to test it but I think it should give you a general idea.
public class ConnectionService extends Service {
private Handler handler = new Handler();
public void restartConnection(){
int sleepTime = reconnectCounter * MIN_RECON_WAIT;
if (sleepTime > MAX_RECON_WAIT)
{
sleepTime = MAX_RECON_WAIT;
}
String msg = "The connection has been lost. Restart attempt will start in: " + sleepTime/1000 + " seconds";
(new Timer()).schedule(
new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show();
reconnectCounter++;
this.startConnectionThread()
}
});
}
}, sleepTime);
}//end restartConnection
}//end ConnectionService
这篇关于Android的吐司开始从服务只显示一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!