android服务 Activity 行为概念检查

标签 android service android-activity

经过一番学习,我总结出以下几点,如有错误请指出。

一个 Activity 通过“onStartCommand”启动一个服务,在服务类中。 onStartCommand() {

return START_NOT_STICKY;

} 因此,有了 START_NOT_STICKY,该服务将永远运行,除非被系统内存不足杀死。

与该服务关联的 Activity 将更容易被终止,让服务单独存在。

以下不太确定: 我可以在任务栏中添加一个图标,在通知页面中添加一个通知 block 。我可以在它们上面添加 onchecklistener。当点击图标时,我必须检查 Activity 是否被广播杀死,然后重新启动 Activity 并更新 View 。

最佳答案

An activity start a service by "onStartCommand"

Activity 不是通过调用 onStartCommand 而是调用 startService() 或 bindService() 来启动服务,因此系统调用 onStartCommand 或 onBind。

With the START_NOT_STICKY, the service will run forever unless being killed by system memory low.

服务用于在没有任何用户界面的情况下在后台执行长时间操作。如果您的服务被终止,您在服务的 onStartCommand 方法中返回的整数决定了系统应该执行的操作。

The activity associated with the service will be get killed more easily, leaving the service alone.

服务没有用户界面。服务将继续运行取决于您是否通过调用 bindService() 或 startService() 启动服务

I could add an icon in task bar and a notification block in notification page.

是的,您可以将通知添加到通知栏。

I can add onchecklistener on them.

您可以在通知上定义和设置 Pending Intent

And when the icon is clicked, I have to check if the activity is being killed by broadcasting, and restart the activity and update the view.

读这个,Starting Activity through notification: Avoiding duplicate activities

关于android服务 Activity 行为概念检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16370711/

相关文章:

Android导航清除整个后台

android - 在付费 Android 应用中使用 LGPL 库

Android:从物理设备连接到模拟器上的套接字服务器

php - Symfony2 : Choose a service depending on an object classname

python - 在后台运行基于 GUI 的应用程序的 .exe

java - 如何从 Tomcat 8 中的 `HttpServletRequest` 获取服务

android - 是否可以通过 adb shell 启动 Activity 并接收其响应?

android mainactivity和服务流程

android - libgdx 点光源不适用于生成的网格

java - 将 String 转换为 double 时 GSON 中的 NumberFormatException