处理程序与AsyncTask对线程(Handler vs AsyncTask vs Thread)
我对Android中的
Handlers
,AsyncTask
和Threads
之间的差异有点困惑。 我在stackoverflow中阅读了不少博客和问题。
Handler
是提供与UI通信的后台线程。 更新进度条例如应该通过Handler
。 使用处理程序,您具有MessagingQueues
的优势,因此,如果您要安排消息或更新多个UI元素或重复任务。
AsyncTask
是类似的,实际上它们使用了Handler
,但是并没有在UI线程中运行,所以它很适合于获取数据,例如获取Web服务。 稍后您可以与UI进行交互。然而,
Thread
不能与UI交互,提供更多的“基本”线程,并且您错过了所有AsyncTask
的抽象。但是我想在服务中运行套接字连接。 应该在一个处理程序或线程中运行,甚至是
AsyncTask
? UI交互根本不是必需的。 在使用性能方面有什么不同吗?同时文件大幅改善。
I got slightly confused about the differences between
Handlers
,AsyncTask
andThreads
in Android. I've read quite a few blogs and questions here in stackoverflow.
Handler
are background threads that provide you to communicate with the UI. Updating a progressbar for instance should be done viaHandler
. Using Handlers you have the advantage ofMessagingQueues
, so if you want to schedule messages or update multiple UI elements or have repeating tasks.
AsyncTask
are similar, infact they make use ofHandler
, but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services. Later you can interact with the UI.
Thread
however can't interact with the UI, provide more "basic" threading and you miss all the abstractions ofAsyncTask
.However I would like to have a socket connection run in a service. Should this be run in a handler or a thread, or even an
AsyncTask
? UI interaction is not necessary at all. Does it make a difference in terms of performance which I use?Meanwhile the documentation has been majorly improved.
原文:https://stackoverflow.com/questions/6964011
最满意答案
你只是错过了从int转换为char的转换。 这条线
str.setCharAt(counter, (char) number);
应该
str.setCharAt(counter, (char) ('0' + number));
You're only missing the conversion from int back to char. The line
str.setCharAt(counter, (char) number);
should be
str.setCharAt(counter, (char) ('0' + number));
相关问答
更多-
将整数转换为字符(convert integer to a character)[2022-05-20]
试试这个: char letter = 'a'; char convertedletter; convertedletter = (char) (letter + 5); System.out.print(convertedletter); 运算符“+”用于添加数字或连接字符串,因为你使用它一个字符(只有一个字符)和一个int它将返回一个int,除非你将它转换为char是你想要的。 try this: char letter = 'a'; char convertedletter; convertedle ... -
你只是错过了从int转换为char的转换。 这条线 str.setCharAt(counter, (char) number); 应该 str.setCharAt(counter, (char) ('0' + number)); You're only missing the conversion from int back to char. The line str.setCharAt(counter, (char) number); should be str.setCharAt(counter, ...
-
如何在Python中将字符转换为整数,反之亦然?(How can I convert a character to a integer in Python, and viceversa?)[2023-09-03]
使用chr()和ord() : >>> chr(97) 'a' >>> ord('a') 97 Use chr() and ord(): >>> chr(97) 'a' >>> ord('a') 97 -
如何在Java编程语言中将ipv6地址转换为整数(how to convert ipv6 address into integer in Java programming language)[2023-11-25]
您必须使用BigInteger ,因为IPv6地址大于Java的本机整数数据类型支持,为128位。 根据您具有IPv6地址的格式(原始字节数组,十六进制字符串......),可能有也可能不是适合的BigInteger构造函数。 You would have to use a BigInteger, since IPv6 addresses are larger than Java's native integer datatypes support, at 128 bits. Depending on in ... -
import java.text.DateFormatSymbols; public String getMonth(int month) { return new DateFormatSymbols().getMonths()[month-1]; } import java.text.DateFormatSymbols; public String getMonth(int month) { return new DateFormatSymbols().getMonths()[month ...
-
没有从int[]到List
转换的快捷方式,因为Arrays.asList不处理拳击,只会创建一个List 这不是你想要的。 你必须做一个实用的方法。 int[] ints = {1, 2, 3}; List intList = new ArrayList (); for (int index = 0; index < ints.length; index++) { intList.add(ints[index]); } There i ... -
您的方法失败,因为您的代码等效于: return (H1 + H2) + ":00"; 所以它在追加字符串之前总结每个数字! 您可以通过执行以下操作来“纠正”(或实际破解)它: return H1 + (H2 + ":00"); 甚至更好,使用String.format : public String formatted(int n) { // print "n" with 2 digits and append ":00" return String.format("%02d:00", ...
-
在SQL Server 2005中将Unicode整数转换为Unicode字符(Convert Unicode Integer into Unicode Character in SQL Server 2005)[2022-05-24]
NChar应该转换回来,取0到65535的范围 Select NChar(36) Returns '$' NChar should convert back, takes a range of 0 through 65535 Select NChar(36) Returns '$' -
Character.toString((char)93); 如果从包含字符9和3的字符串开始,请使用Integer.parseInt() 。 Character.toString((char)93); Use Integer.parseInt() if you are starting with a string containing the characters 9 and 3.
-
Character.forDigit(C, 10) 试试这个,它应该工作。 Character.forDigit(C, 10) Try this, it should work.