广告
返回顶部
首页 > 资讯 > 移动开发 >Android Service中使用Toast无法正常显示问题的解决方法
  • 886
分享到

Android Service中使用Toast无法正常显示问题的解决方法

androidserviceservice方法toastAndroid 2022-06-06 07:06:15 886人浏览 八月长安
摘要

本文实例讲述了Android Service中使用Toast无法正常显示问题的解决方法。分享给大家供大家参考,具体如下: 在做Service简单练习时,在Service中的On

本文实例讲述了Android Service中使用Toast无法正常显示问题的解决方法。分享给大家供大家参考,具体如下:

在做Service简单练习时,在Service中的OnCreate、OnStart、OnDestroy三个方法中都像在Activity中同样的方法调用了Toast.makeText,并在Acitivy中通过两个按钮来调用该服务的onStart和onDestroy方法:

DemoService代码如下:


@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();
}

运行之后,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:


public void show() {
    ...
    service.enqueueToast(pkg, tn, mDuration);  //将该toast插入到一个消息队列中
    ...
}

原理上看,Android中大致上是消息队列和消息循环,主线程从消息队列中取得消息并处理。而Handler看作是一个工具类,用来向消息队列中插入消息。所以我们重构原来的代码:


@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();
      }
    });
}

运行之后的效果如下:

总结:在Android的Framework中使用Toast,要将Toast添加到主线程里才能正常工作。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作sqlite数据库技巧总结》、《Android操作JSON格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android资源操作技巧汇总》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android使用Toast显示消息提示框超简单实现Android自定义Toast示例(附源码)Android Toast的用法总结(五种用法)Android 5.0以上Toast不显示的解决方法android开发教程之实现toast工具类android自定义toast(widget开发)示例Android 彩色Toast的实现代码


--结束END--

本文标题: Android Service中使用Toast无法正常显示问题的解决方法

本文链接: https://www.lsjlt.com/news/23494.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作