Android:在Service中使用Toast
来源:互联网 发布:淘宝刷收藏 编辑:程序博客网 时间:2024/05/21 09:06
这个再次说明,所有涉及UI的更新的操作,必须放在UI线程中。
转自:http://bbs.chinaunix.net/thread-3653294-1-1.html
在Service中Toast,本来以为是和在Activity中一样直接用,结果发现没有反应,百度到的解决办法,原理暂时不清楚。
Try using a Handler. Thing about Toasts is, you have to run makeText on the UI thread, which the Service doesn't run on. A Handler allows you to post a runnable to be run on the UI thread. In this case you would initialize a Handler in your onStartCommand method.
private Handler mHandler;@OverrideonStartCommand(...) { mHandler = new Handler();}private class ToastRunnable implements Runnable { String mText; public ToastRunnable(String text) { mText = text; } @Override public void run(){ Toast.makeText(getApplicationContext(), mText, Toast.LENGTH_SHORT).show(); }}private void someMethod() { mHandler.post(new ToastRunnable(<putTextHere>);}
另一个方法:http://www.androidsnippets.com/use-toast-wherever-you-want
// A class that will run Toast messages in the main GUI context private class ToastMessageTask extends AsyncTask<String, String, String> { String toastMessage; @Override protected String doInBackground(String... params) { toastMessage = params[0]; return toastMessage; } protected void OnProgressUpdate(String... values) { super.onProgressUpdate(values); } // This is executed in the context of the main GUI thread protected void onPostExecute(String result){ Toast toast = Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT); toast.show(); }}// to use: new ToastMessageTask().execute("This is a TOAST message!");raw ·· download
0 0
- [Android] 在Service中使用Toast
- android 在service中使用toast
- Android:在Service中使用Toast
- 在Service中使用Toast
- 在IntentService中使用Toast与在Service中使用Toast的异同
- 在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast
- Android:Toast在Service组件中实现其显示功能
- Android开发之在service 里面使用Toast
- Handler: Service中使用Toast
- Handler: Service中使用Toast
- Handler: Service中使用Toast
- 在Android中使用Toast进行提示
- Android Studio 在活动中使用Toast
- 在Android Service组件里面弹出Toast
- Toast 在service中出现的问题
- Android学习-Service中显示Toast
- Service中使用Toast显示问题
- Service中使用Toast显示问题
- 第二次 重装系统 感悟
- centos6.3 安装(openerp)oe7的脚本
- SQLServer存储过程返回值总结
- 小P的故事——神奇的饭卡
- linux下设置oracle自启动
- Android:在Service中使用Toast
- socket 编程汇总
- FFmpeg解码视频和SDL显示
- java抽象类
- #读文笔记#马化腾:用户体验的10/100/1000法则
- 软件工程中的那些图:系统流程图,程序流程图和数据流图
- FFmpeg解码音视频
- Retina MBP的Windows虚拟机视网膜屏的显示效果问题总结
- java打印