[Android] Service和IntentService中显示Toast的区别
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);
}
[Android] Service和IntentService中显示Toast的区别的更多相关文章
- Android学习笔记----TimerTask中显示Toast的问题
今天想在TimerTask的run函数中调用Toast显示一下提示信息,却总是导致程序崩溃.可是try语句块却又无法捕获到异常,代码如下: ...... Timer timer = new Timer ...
- 在IntentService中使用Toast与在Service中使用Toast的异同
1. 表象 Service中能够正常显示Toast,IntentService中不能正常显示Toast.在2.3系统上,不显示toast,在4.3系统上,toast显示.可是不会消失. 2. 问题分析 ...
- Android 高级UI设计笔记17:Android在非UI线程中显示Toast
1. 子线程的Toast怎么显示不出来? 因为Toast在创建的时候会依赖于一个Handler,并且一个Handler是需要有一个Looper才能够创建,而普通的线程是不会自动去创建一个Looper对 ...
- Android Service、IntentService,Service和组件间通信
Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- Android Service 与 IntentService
Service 中的耗时操作必须 在 Thread中进行: IntentService 则直接在 onHandleIntent()方法中进行
- Android Studion的Monitor中显示No Debuggable Application的解决方法
在使用Android Studion的时候,突然android Monitor中无法下拉显示调试项目,只是一直提示No Debuggable Application,然后上网搜索的解决办法: 第一种方 ...
随机推荐
- 网站开发常用jQuery插件总结(三)拖拽插件gridster
1.gridster插件功能 实现类似于win8 磁贴拖拽的功能 2.gridster官方地址 http://gridster.net/ 在官方的网站上也有插件的帮助和实例,但是按照官方的说明,我在本 ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- 将Excel导入到数据中
常用的方式的有两种: 1. 通过 Microsoft.Jet.OLEDB.4.0 或 Microsoft.ACE.OLEDB.12.0 Microsoft.ACE.OLEDB.12.0 需要安装 A ...
- 15.禁用ViewState
默认情况下ASP.net是启用ViewState的,这样在页面中会生成冗长的隐藏字段,ViewState对于需要PostBack处理的页面才可能有用,对于不需要交互的页面则完全没有必要用ViewSta ...
- github Travis CI 持续集成
一个项目如何保证代码质量是开发中非常重要的环节,对于开源项目来说更是如此,因为开源项目要面对的是来自不同水平开发者提交的代码.所以围绕开源做持续集成(Continuous Integration)变得 ...
- jsoup技术抓取网页数据大全
jsoupNews Bugs Discussion Download API Reference Cookbook jsoup ? Cookbook ? Extracting data ? 使用选择器 ...
- 华为5700交换机通过外部开源protal和本地aaa用户认证的一些问题
http://support.huawei.com/ecommunity/bbs/10178271.html?p=1#p0 华为5700交换机通过外部开源protal和本地aaa用户认证的一些问题 各 ...
- [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...
- Cut the Cake(大数相乘)
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...
- 「Poetize8」Divisible
描述 Description 设F[i]为斐波那契数列的第i项,F[1]=1,F[2]=1,F[i]=F[i-1]+F[i-2](i>=3). 输入格式 InputFormat 输入包含若干行( ...