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. js事件练习--登录界面演示。

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. HTML5 表单与文件

    -新增元素与属性 form.formaction.formmethod.placeholder(处于未输入状态时文本框显示的输入提示).autofocus(自动获取光标焦点).list(该属性的值为某 ...

  3. QQ空间API接口

    (以下内容可能会随着时间改变而改变!) 查看对方QQ空间的背景音乐 http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?json=0& ...

  4. python类class基础

              44.class类:                      一.类定义的一般形式:                            1.简单的形式:实例化对象没有自己独有 ...

  5. openerp import l field size limit

    modify the file addons/base_import/models.py add the following line at the very begining of the _con ...

  6. 练习2 D 题- 第几天?

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 给定一个日 ...

  7. NSDictionary 使用总结

    NSDictionary使用小结 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @ ...

  8. Ruby自学笔记(五)— 条件判断

    条件判断,在编程语言中都存在,而Ruby中的条件判断和Java中类似,当然还是存在些许差别 Ruby中条件判断的条件: 1) 可以使用 ==,<,>等比较运算来作为条件,比较运算可以返回t ...

  9. MVC身份验证及权限管理

    MVC自带的ActionFilter 在Asp.Net WebForm的中要做到身份认证微软为我们提供了三种方式,其中最常用的就是我们的Form认证,需要配置相应的信息.例如下面的配置信息: < ...

  10. STM32自带的bool型变量

    这些标着位会让你的程序使用起来更方便. First you need to include "STM32f10x_type.h" /*布尔型变量*/typedef enum{FAL ...