最佳实践与后台服务发展的活动 [英] Best Practices for developing an Activity with a background Service

查看:215
本文介绍了最佳实践与后台服务发展的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序对用户界面和后台轮询一个有趣的服务一个活动。好像标准收费。


  1. AlarmManager能引发不活动的OnCreate被调用的服务意图是什么?


  2. 有没有办法把活动与放任何好处;服务于不同的应用程序?这会创建2 apk文件的,并使其无法投入市场作为一个应用程序?你可以把2应用到一个清单不知何故?


  3. 对于双方的沟通:


- 如果活性放大器;服务是相同的应用程序的一部分 - 我不能只是在适用范围存储公共对象(如用户对象)2分享?

- 它好像我甚至都不需要AIDL打扰 - 两个可能只需要在应用范围内给对方弱引用,以及 - 他们可以调用对方的方法呀?还是应该的pub / sub彼此具有某种Observer模式或BroadcastListener的事情吗?


解决方案

  

能否AlarmManager引发不活动的OnCreate被调用的服务意图是什么?



  

有没有办法把活动与放任何好处;服务于不同的应用程序?


恕我直言,没有。


  

这会创建2 apk文件的,并使其无法投入市场作为一个应用程序?



  

您可以把2应用到一个清单不知何故?


从纯XML的角度来看,有房在清单中的多个<应用> 元素。然而,AFAIK,只有一个被支持。


  

如果活性放大器;服务是相同的应用程序的一部分 - 我不能只是存储在应用范围内常用的对象(如用户对象)2分享


有关非常快的东西,是的。但是,请记住,你的服务可能会关闭(由Android,用户等),在这之后你的进程可能会终止,你的应用对象超出的。我想用这个只光缓存。


  

好像我甚至都不需要AIDL费心


正确的 - 即只需要进程间服务绑定


  

两个可能只是在适用范围都彼此弱引用以及


我不会做,在一万年。请负责任地使用该平台。有很多的活动和服务,但沟通依然松散耦合(或在本地绑定的模式,紧耦合在Android感知时尚的情况下)的方式。


  

还是应该的pub / sub彼此具有某种Observer模式或BroadcastListener事?


类似的规定将是preferable。而活性和服务可以是共驻留在同一时间在同一过程中,它们不是设计成直接连接到彼此。

My Application has an Activity for the UI and a Service for background polling fun. Seems like standard fare.

  1. Can AlarmManager trigger the Service Intent without Activity OnCreate being called?

  2. Is there any benefit to putting the Activity & Service into different Applications? Would this create 2 apk's and make it impossible to put into Market as one app? Can you put 2 applications into one manifest somehow?

  3. Regarding communication between the two:

-If Activity & Service are part of the same Application - can't I just store common objects (like User object) at the Application scope for the 2 to share?

-It seems like I don't even need to bother with AIDL - the two could just have weak references to each other at the Application scope as well - and they can call methods on each other that way? Or should they pub/sub each other with some kind of Observer Pattern or BroadcastListener thing?

解决方案

Can AlarmManager trigger the Service Intent without Activity OnCreate being called?

Yes.

Is there any benefit to putting the Activity & Service into different Applications?

IMHO, no.

Would this create 2 apk's and make it impossible to put into Market as one app?

Yes.

Can you put 2 applications into one manifest somehow?

From a pure XML standpoint, there is room in the manifest for more than one <application> element. However, AFAIK, only one is supported.

If Activity & Service are part of the same Application - can't I just store common objects (like User object) at the Application scope for the 2 to share?

For very quick things, yes. However, bear in mind that your service may get shut down (by Android, by user, etc.), after which your process may get terminated, and your Application object goes poof. I'd use this for light caching only.

It seems like I don't even need to bother with AIDL

Correct -- that is only needed for inter-process service binding.

the two could just have weak references to each other at the Application scope as well

I wouldn't do that in a million years. Please use the platform responsibly. There are plenty of ways for activities and services to communicate yet remain loosely coupled (or, in the case of the local binding pattern, tightly-coupled in an Android-aware fashion).

Or should they pub/sub each other with some kind of Observer Pattern or BroadcastListener thing?

Something along those lines would be preferable. While the activity and the service may be co-resident in the same process at the same time, they are not designed to be directly linked to one another.

这篇关于最佳实践与后台服务发展的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆