1. 表象

    Service中可以正常显示Toast,IntentService中不能正常显示Toast,在2.3系统上,不显示toast,在4.3系统上,toast显示,但是不会消失。

2. 原因

    Toast要求运行在UI主线程中。
    Service运行在主线程中,因此Toast是正常的。
    IntentService运行在独立的线程中,因此Toast不正常。

3. 在IntentService中显示Toast

    利用Handler,将显示Toast的工作,放在主线程中来做。具体有两个实现方式。

    Handler的post方式实现,这个方式比较简单。
    private void showToastByRunnable(final IntentService context, final CharSequence text, final int duration)     {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, text, duration).show();
}
});
}

Handler的msg方式实现,这个方式比较复杂。

    Handler msgHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Toast.makeText(ToastIntentService.this, msg.getData().getString("Text"), Toast.LENGTH_SHORT).show();
super.handleMessage(msg);
}
};
private void showToastByMsg(final IntentService context, final CharSequence text, final int duration) {
Bundle data = new Bundle();
data.putString("Text", text.toString());
Message msg = new Message();
msg.setData(data);
msgHandler.sendMessage(msg);
}
4. 关于耗时操作
    
    Service中如果有耗时的操作,要开启一个Thread来做。
    IntentService是在独立的线程中,所以可以进行一些耗时操作。

5. 考虑AsyncTask与Service的使用区别
    
    如果是全后台的工作,使用Service,结果的提示可以使用Notification。
    如果是异步工作,工作结束后需要更新UI,那么最好使用Thread或者AsyncTask。

6. 参考

[Android] Service和IntentService中显示Toast的区别的更多相关文章

  1. Android学习笔记----TimerTask中显示Toast的问题

    今天想在TimerTask的run函数中调用Toast显示一下提示信息,却总是导致程序崩溃.可是try语句块却又无法捕获到异常,代码如下: ...... Timer timer = new Timer ...

  2. 在IntentService中使用Toast与在Service中使用Toast的异同

    1. 表象 Service中能够正常显示Toast,IntentService中不能正常显示Toast.在2.3系统上,不显示toast,在4.3系统上,toast显示.可是不会消失. 2. 问题分析 ...

  3. Android 高级UI设计笔记17:Android在非UI线程中显示Toast

    1. 子线程的Toast怎么显示不出来? 因为Toast在创建的时候会依赖于一个Handler,并且一个Handler是需要有一个Looper才能够创建,而普通的线程是不会自动去创建一个Looper对 ...

  4. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  5. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  6. 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  7. 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  8. Android Service 与 IntentService

    Service 中的耗时操作必须 在 Thread中进行: IntentService 则直接在 onHandleIntent()方法中进行

  9. Android Studion的Monitor中显示No Debuggable Application的解决方法

    在使用Android Studion的时候,突然android Monitor中无法下拉显示调试项目,只是一直提示No Debuggable Application,然后上网搜索的解决办法: 第一种方 ...

随机推荐

  1. ICE学习第一步-----配置ICE环境变量

    安装 ICE: 1.下载ICE: http://www.zeroc.com/download.html 下载说明:ICE支持语言(C++, Java, C#, Visual Basic,Python, ...

  2. 知识库总结mysql常用cmd命令

    打开命令目录 打开D盘mysql目录 d: cd D:\Ampps\mysql\bin 常用操作 将mysql目录下bin目录中的mysql.exe放到C:\WINDOWS下,可以执行以下命令 连接: ...

  3. Android LinearLayout中weight属性的意义与使用方式

    layout_weight 分割父级容器的比例

  4. python中归并排序

    # coding=UTF-8 #!/usr/bin/python import sys def merge(nums, first, middle, last): "merge" ...

  5. Python Tutorial 学习(三)--An Informal Introduction to Python

    3.1. 将Python用作计算器 3.1.1. Numbers 数 作为一个计算器,python支持简单的操作, '+','-','*','/'地球人都知道的加减乘除. ()可以用来改变优先级,同数 ...

  6. OBJC运行时方法替换(Method swizzling)

    在上周associated objects一文中,我们开始探索Objective-C运行时的一些黑魔法.本周我们继续前行,来讨论可能是最受争议的运行时技术:method swizzling.   Me ...

  7. BZOJ 1020 安全的航线flight

    Description 在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞 ...

  8. SHell命令总结

    cat  files-to-copy.txt | xargs -i cp {} /tmp

  9. SetTimer and CreateWaitableTimer的例子(静态函数设置为回调函数,瑞士的网页,有点意思)

    Timers (SetTimer and CreateWaitableTimer) in Windows   SetTimer The following example creates a time ...

  10. 一个用UpdateLayeredWindow实现窗体半透明的delphi的代码

    http://www.pudn.com/downloads171/sourcecode/windows/detail791686.html unit Unit1; interface  uses   ...