我应该使用 android: process =“:remote"吗?在我的接收器中? [英] Should I use android: process =":remote" in my receiver?

查看:40
本文介绍了我应该使用 android: process =“:remote"吗?在我的接收器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个广播接收器,它经常被调用,我注意到很多人使用

I have a BroadcastReceiver which is called every so often, and I have noticed many people use

android: process =":remote" 

在他们的接收器中.我的用于检查一些事情,如果条件匹配则激活警报.我的问题是我应该使用我在清单中上面发布的行吗?如果是这样,这样做有什么好处?

in their receiver. Mine is used to check a few things and if the conditions match then activate an alarm. My question is should I use the line I had posted above in my manifest? And if so what are the benefits of doing so?

推荐答案

通过使用 android:process=":remote" 定义接收器,您基本上可以在不同的进程 (= VM) 中运行接收器.对于典型的用例,您不需要在不同的进程中运行它,无论您想做什么,都可以在本地(在您的 APK 进程中)正常运行.

By defining your receiver with android:process=":remote" you basically run your receiver in a different process (= VM). For typical use-cases, you don't need to run this in a different process, and whatever you want to do can probably run just fine locally (in your APK process).

使用 android:process=":remote" 的缺点是您需要额外的资源才能运行它(在这种情况下是一个单独的进程).这样做时,您基本上是在处理 2 个虚拟机,并且某些模式(例如单例、静态字段)无法再在您的应用和远程服务之间共享.

The drawback of using android:process=":remote" is that you need additional resources for it to run (in this case a separate process). When doing so, you're basically dealing with 2 VMs, and some patterns like singletons, static fields can no longer be shared between your app and your remote service.

使用 android:process=":remote" 的好处是,对于某些用例,启动一个将在之后继续运行(在其自己的进程中)的服务可能很方便您已经关闭了您的应用程序,或者您希望远程客户端能够绑定到您的服务.通过调用 onReceive() 方法在单独的进程中运行时,您的广播接收器不会阻塞您的应用程序主线程(但是,还有其他实现方法).

The benefit of using android:process=":remote" is that for some use-cases, it might be handy to start a service that will keep on running (in its own process) after you've shutdown your application, or if you want remote clients to be able to bind to your service. Your broadcast receiver will not block your applications main thread when running in a separate process upon calling the onReceive() method (however, there are other ways of implementing this).

我发现大多数情况下,对于大多数常见用例,您无需使用 android:process=":remote" 即可逃脱.

I've found that most of the time, for most common use-cases, you can get away without using android:process=":remote".

这篇关于我应该使用 android: process =“:remote"吗?在我的接收器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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