Android 的服务和线程
问题说明
我正在寻找应该在 android 应用程序中使用什么服务.
I am looking for what service should be used in android applicaton.
文档说
服务是一个应用程序组件,可以在后台执行长时间运行的操作,并且不提供用户界面.
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
我读过这个线程 应用线程 vs 服务线程 说相同的服务是用于在后台运行操作.
I have read this thread Application threads vs Service threads that saying same services are for running operation in background.
但是这里也可以使用 Thread
来完成.它们之间的任何区别以及您应该在哪里使用它们
But here this can be done using Thread
also. Any difference between them and where you should use them
正确答案
服务旨在独立于 Activity
运行您的任务,它允许您在后台运行任何任务.它在主 UI 线程上运行,因此当您想要执行任何网络或高负载操作时,您必须在那里使用 Thread
.
A Service is meant to run your task independently of the Activity
, it allows you to run any task in background. This run on the main UI thread so when you want to perform any network or heavy load operation then you have to use the Thread
there.
示例:假设您想每天在后台备份即时消息,那么在这里您将使用 Service
.
Example : Suppose you want to take backup of your instant messages daily in the background then here you would use the Service
.
Threads
用于在它自己的线程而不是主 UI 线程中运行您的任务.当您想要执行一些繁重的网络操作(例如连续向服务器发送字节)时,您会使用它,并且它与 Android 组件相关联.当您的组件破坏了谁启动它时,您也应该停止它.
Threads
is for run your task in its own thread instead of main UI thread. You would use when you want to do some heavy network operation like sending bytes to the server continuously, and it is associated with the Android components. When your component destroy who started this then you should have stop it also.
示例:您在 Activity 中使用 Thread
出于某种目的,当您的 Activity 销毁时停止它是一种很好的做法.
Example : You are using the Thread
in the Activity for some purpose, it is good practice to stop it when your activity destroy.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjaib
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20