📜  Android中前台服务与活动之间的区别

📅  最后修改于: 2022-05-13 01:58:10.793000             🧑  作者: Mango

Android中前台服务与活动之间的区别

服务是应用程序的一个组件,它可以在后台执行长时间运行的活动。它没有图形用户界面。在用户切换到另一个程序后,已经启动的服务可能会继续运行一段时间。组件还可以绑定到服务以与其通信,甚至执行进程间通信 (IPC)。例如,服务可以在后台进行网络交易、播放音乐、执行文件 I/O 以及与内容提供者通信。让我们以更细化的方式进一步理解它:

差异

Foreground service

Activity

A foreground service executes an action that is visible to the user. A foreground service, for example, would be used by an audio app to play an audio track.A single, concentrated item that the user may accomplish is referred to as an activity.
A Notification must be displayed by foreground services. Even when the user is not interacting with the app, foreground services continue to execute.All the activities enlisted in the android manifest are meant to be used by the user and hence they are in front while being executed. 
You must give notice when utilizing a foreground service so that users are aware that the service is functioning. This is done through notifications that are not dismissableWhen using an activity for your android app, there is no need to provide notification as it resides as full glanceable content. 
This message will not go away unless the service is either terminated or removed from the foreground.The activities in android can also be used in mini modes and can be resizable. 
When a service is launched, it has its own lifecycle that is unrelated to the component that launched it. Even if the component that launched it is removed, the service can continue to operate in the background indefinitely.Activity stacks are used to manage activities in the system. When a new activity is launched, it is usually added to the top of the existing stack and becomes an active activity.
A service that has been launched must manage its own lifetime. That is unless the system needs to recover system memory, the system does not terminate or delete the service, and the service continues to operate after onStartCommand().An activity need not manage its own lifecycle as the Android system could actually manage it if it needs more memory
When another component uses startService, the service is created (). The service will then continue indefinitely and must be stopped by using stopSelf ()The startActivity(Intent) function is used to launch a new activity, which is added to the top of the activity stack. It only accepts one parameter, an Intent that describes the action to be performed.

结论

服务是 Android 中的一个独特组件,它允许应用程序在后台运行以执行长时间运行的操作活动,另一方面,活动,如Java中的窗口或框架,代表具有用户界面的单个屏幕。 Android 活动类是 ContextThemeWrapper 类的子类。