安卓:做应用程序登录背景就开机 [英] Android : Do Application Login in background on boot-up

查看:131
本文介绍了安卓:做应用程序登录背景就开机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VOIP 应用程序,我需要登录后台应用程序在设备上启动。

I have a VOIP Application, I need to login the application in background on device bootup.

目前在init我的应用程序在 UI 活动(<$ C C $>的onCreate())。

Currently the init to my application is done on UI Active(onCreate()).

我在我的脑海下面的事情,任何人都可以帮助并清除我的疑惑。

I have the following things in my mind, can anyone help and clear my doubts.

  1. 在服务设计是必须要完成此任务??
  2. 其中服务远程(AIDL)或本地服务,为什么?
  3. 如何在 UI 服务交互发生?
  4. UI 活跃谁得到Call-腰板? UI 服务
  5. 我应该让服务我的控制器服务 UI 数据传递反之亦然?
  1. The service design is must to achieve this task??
  2. Which Service Remote(AIDL) or Local Service and why?
  3. How does the UI and Service interaction happens?
  4. After UI is active who gets the Call- Backs? UI or Service?
  5. Should i make Service as my Controller i.e Service to UI data Pass Vice-versa?

示例应用程序:Skype的

Sample App: Skype.

推荐答案

因此​​,有很多方法可以实现你想要什么,这是一个什么适合你的风格和设计更好的事。希望你会发现这些信息很有用。

So there are many ways to achieve what you want, it is a matter of what fits your style and design better. Hopefully you will find this information useful.

  1. 有关应用程序在启动时有几个选项的背景登录。你需要的第一件事是它被定义为在清单中接收一个BroadcastReceiver。拥有的BroadcastReceiver捕捉 ACTION_BOOT_COMPLETED 的意图。从这里,你可以启动你的服务。这将导致#2。

  1. For the application to login in the background on startup there are a few option. The first thing you will need is a BroadcastReceiver which is defined as a receiver in the manifest. Have the BroadcastReceiver catch the ACTION_BOOT_COMPLETED intent. From here you can launch your Service. This leads to #2.

如果你正在做的是基于REST调用,那么一个真正的IntentService将是理想的。一个 IntentService 和的服务很简单:一个IntentService使用主线程,执行它的'code'和死亡。一个服务,在主线程中运行,但(这是一个重要的事实),并且是长时间运行,因此必须被告知的 stopSelf()。为了进一步采取事态,服务也不太可能比一个活动被杀​​害(应用程序组件被杀害,以腾出空间在内存中新推出的应用程序),即。它需要更高的precedence。该服务还可以声明一个<一个href="http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29"相对=nofollow>前台服务,这需要一个通知,但给予更高的precedence。我想在你的情况下,服务将是完美的。

If all you are doing are RESTful calls then really an IntentService would be ideal. The difference between an IntentService and a Service is simple: An IntentService runs off of the main thread, executes it's 'code' and dies. A Service, however runs on the main thread (this is an important fact) and is long running so it has to be told to stopSelf(). To take matters further, a Service is also less likely to be killed compared to an Activity (application components are killed to make room in memory for newly launched apps), ie. it takes higher precedence. The service can also be declared a foreground service which requires a notification but give even higher precedence. I think in your case a Service would be perfect.

在你的UI(活动)打开连接到该服务将是最好的办法<一href="http://developer.android.com/reference/android/app/Service.html#onBind%28android.content.Intent%29"相对=nofollow>粘合剂。这将允许从不同的应用程序/组件的多个接口的服务,如果需要的话。 AIDL是pretty的很酷的东西,但是从我的经验,更难管理,因为所有的参数必须是原始的或Parcables。 AIDL也是较慢的效率较低,因为它是真正的IPC的一种形式。当开始与意图一个服务的onStartCommand()方法被调用。如果该服务是通过一个应用程序试图绑定到它开始则onBind()方法被调用。但是你可以启动服务,和意图,然后绑定到它。如果preFER RESTful方法,你只可以快速调用数据,你可以使用IntentService用的 ResultReceiver 。 <一href="http://neilgoodman.net/2012/01/01/modern-techniques-for-implementing-rest-clients-on-android-4-0-and-below-part-2/"相对=nofollow>这是写的关于谷歌I / O的例子,只是整体执行情况良好,如果你有兴趣的IntentService和ResultReceiver一大篇。

Once your UI (Activity) is opened the best way to connect to the Service would be the Binder. This will allow multiple interfaces to the Service from different applications / components if need be. AIDL is pretty cool stuff but from my experience much harder to manage since all parameters must be primitive or Parcables. AIDL is also slower an less efficient because it is really a form of IPC. When a Service is started with an intent the onStartCommand() method is called. If the service is started by an application trying to bind to it then the onBind() method is called. But you can start the Service with and Intent and then bind to it. If you prefer the RESTful approach where you just have quick calls for data you can use an IntentService with a ResultReceiver. This is a great article written about Google I/O examples and just overall well implemented if you are interested in the IntentService and ResultReceiver.

这是给你。使用粘合剂或AIDL你的活动可以调用服务的方法,就像对象的方法,其中的回调也只是方法返回。如果您使用的是ResultReceiver活动接口的接收器将回调。你也可以只通过意图来回,但是这可能会导致混乱。再次感谢您的情况下,活页夹的做法是很好的,以及一个接收器。

This is up to you. Using the Binder or AIDL your Activity can call the Service methods just like object method where the 'callback' would just be the method return. If you use a ResultReceiver the Activity interfacing the Receiver would be the callback. You could also just pass Intents back and forth but this could get messy. Again for your case the Binder approach would be good as well as a Receiver.

想想服务作为MVVM系统模型 - 使用它作为一个辅助,以获取数据,而不是一些控制应用程序的逻辑

Think of the Service as a model in the MVVM system - use it as a helper to get data from, not as something that controls the logic of the application.

很抱歉,如果这似乎凌乱有这么多的方式来实现你所期待的。你的情况最好的是什么适合你的感觉是什么它只是一个问题比较好。且不说Android的SDK是pretty的大。我试图打在所有能帮助你的主题。祝你好运!

Sorry if this seems messy there are so many ways to achieve what you are looking for. Its just a matter of what fits your situation best what you 'feel' is better. Not to mention the Android SDK is pretty large. I tried to hit on all the topics that could help you out. Good luck!

这篇关于安卓:做应用程序登录背景就开机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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