Service中使用Toast显示问题
2017-08-17 13:25
369 查看
作者:陈旭
在做Service简单练习时,在Service中的OnCreate、OnStart、OnDestroy三个方法中都像在Activity中同样的方法调用了Toast.makeText,并在Acitivy中通过两个按钮来调用该服务的onStart和onDestroy方法:
DemoService代码如下:
[java] view
plain copy
<span style="font-size:16px;">@Override
public void onCreate()
{
super.onCreate();
Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
}
</span>
运行之后,DemoService中的信息都没有显示出来。
刚开始以为所得到的Context不正确,在Service直接调用getApplicationContext()得到的是Service的Context,但是细究来看,Toast应该得到主UI的Context才能显示,所以找了一下,Google对Toast的说明中,有一句:
“A toast can be created and displayed from an Activity or Service.
If you create a toast notification from a Service,it appears in front of the Activity currently in focus.”
(http://developer.Android.com/guide/topics/ui/notifiers/toasts.html)
那么按照这句来看,service中创建的toast会在Acivity的UI前面聚焦显示。但为什么运行没有效果呢?再来查看一下makeText方法。
果然还是Context的问题,所以想要toast能够正常工作,需要在Activity的主线程上运行才行,那么如何得到主线程UI的Context呢?可以通过Handler将一个自定义的线程运行于主线程之上。
再来看一下Toast.show方法的src:
[java] view
plain copy
<span style="font-size:16px;">public void show() {
...
service.enqueueToast(pkg, tn, mDuration); //将该toast插入到一个消息队列中
...
}
</span>
原理上看,android中大致上是消息队列和消息循环,主线程从消息队列中取得消息并处理。而Handler看作是一个工具类,用来向消息队列中插入消息。所以我们重构原来的代码:
[java] view
plain copy
<span style="font-size:16px;">@Override
public void onCreate()
{
super.onCreate();
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onDestroy(){
super.onDestroy();
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
}
});
</span>
运行之后的效果如下:
总结:在Android的Framework中使用Toast,要将Toast添加到主线程里才能正常工作
在做Service简单练习时,在Service中的OnCreate、OnStart、OnDestroy三个方法中都像在Activity中同样的方法调用了Toast.makeText,并在Acitivy中通过两个按钮来调用该服务的onStart和onDestroy方法:
DemoService代码如下:
[java] view
plain copy
<span style="font-size:16px;">@Override
public void onCreate()
{
super.onCreate();
Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
}
</span>
运行之后,DemoService中的信息都没有显示出来。
刚开始以为所得到的Context不正确,在Service直接调用getApplicationContext()得到的是Service的Context,但是细究来看,Toast应该得到主UI的Context才能显示,所以找了一下,Google对Toast的说明中,有一句:
“A toast can be created and displayed from an Activity or Service.
If you create a toast notification from a Service,it appears in front of the Activity currently in focus.”
(http://developer.Android.com/guide/topics/ui/notifiers/toasts.html)
那么按照这句来看,service中创建的toast会在Acivity的UI前面聚焦显示。但为什么运行没有效果呢?再来查看一下makeText方法。
果然还是Context的问题,所以想要toast能够正常工作,需要在Activity的主线程上运行才行,那么如何得到主线程UI的Context呢?可以通过Handler将一个自定义的线程运行于主线程之上。
再来看一下Toast.show方法的src:
[java] view
plain copy
<span style="font-size:16px;">public void show() {
...
service.enqueueToast(pkg, tn, mDuration); //将该toast插入到一个消息队列中
...
}
</span>
原理上看,android中大致上是消息队列和消息循环,主线程从消息队列中取得消息并处理。而Handler看作是一个工具类,用来向消息队列中插入消息。所以我们重构原来的代码:
[java] view
plain copy
<span style="font-size:16px;">@Override
public void onCreate()
{
super.onCreate();
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is created!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is on!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onDestroy(){
super.onDestroy();
handler=new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Service is off!", Toast.LENGTH_LONG).show();
}
});
</span>
运行之后的效果如下:
总结:在Android的Framework中使用Toast,要将Toast添加到主线程里才能正常工作
相关文章推荐
- Android Service中使用Toast无法正常显示问题的解决方法
- Service中使用Toast显示问题
- android 使用Dialog实现Toast效果,解决有的机型不显示Toast的问题(保证能用)
- Service中使用Toast出现问题及解决方法
- Android在服务service里使用Toast显示和弹出Dialog
- Toast重复显示问题 自学使用
- 使用Service服务后台运行Toast显示小火箭
- IOS 使用 ArcGis sdk 在iphone 6P上无法显示 AGSDynamicMapServiceLayer 图层的问题
- Service中使用Toast,及遇到的问题
- Android问题—Toast不显示的原因和在子线程中使用方法以及Looper类详细解析
- 使用Report Service发现的一些问题
- FLEX 初学笔记之一,使用HTTPService读取Data.xml文件,显示于表示层
- 母板页图片使用相对路径显示问题
- 使用Report Service发现的一些问题
- Reporting Service Tips 101(#3) - 关于使用Sum函数会遇到的问题(2)
- 在flex中使用嵌入html文件以解决显示html的问题
- vs2008与IIS 7.0使用在vista上时出现的问题及解决方法(Internet Explorer 无法显示该页面)(VS2008: IE Cannot Display Web Page)
- 使用struts bean:write标签时不能显示Integer,BigDecimal等类型的属性问题的解决
- 解决使用VC运行时库函数wprintf和wcount显示中文不正确的问题
- 彻底解决RedHat8下的Kylix3安装使用的问题(安装、字体显示、bcb编译问题)