使用/创建PendingIntent的最简单方法 [英] Simplest way to use / create a PendingIntent
问题描述
也许有些简单的东西我看不到.我正在尝试从某些API调用方法,并且此方法要求PendingIntent异步向我发送一些值.我已经阅读了半个小时的文档和示例,但无法用一种简单的方法来解决问题.
Maybe there's something really simple that I don't see. I'm trying to call a method from some API, and this method asks for a PendingIntent to send me asynchronously some value. I've read the doc and examples for half an hour, and can't wrap my head around a simple way to do it.
更准确地说,我想调用ActivityRecognitionApi. 为了说明,这是我如何从类似的api接收位置的信息:
More precisely, I want to call the ActivityRecognitionApi. To illustrate, here's how I receive the location from a similar api:
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
mLocationRequest,
new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// I can do something with the location
}
});
所以我想做同样的事情来接收公认的活动:
So I want to do the same to receive the recognized activity :
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient,
1000,
// ... what here ?);
那么,创建一个仅接收值的未决Intent的简单方法是什么?我试图创建一个匿名的PendingIntent,但是该类是最终的.我不想启动服务"或活动",也不想广播任何内容……是否只有一种简单的方法来创建此PendingIntent,以仅执行一段代码?
So what would be the simple way to create a pendingIntent to just receive the value ? I tried to create an anonymous PendingIntent but the class is final. I don't want to start a Service, or Activity, or broadcast anything... Is there just a simple way to create this PendingIntent to execute just a block of code?
预先感谢
推荐答案
我不想启动服务或活动,也不想广播任何内容...
I don't want to start a Service, or Activity, or broadcast anything...
那么您将无法使用ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates()
.
是否只有一种简单的方法可以创建此PendingIntent以仅执行一段代码?
Is there just a simple way to create this PendingIntent to execute just a block of code?
否.
此API的常规用例是找出一段时间内设备状态的变化,并且您的进程可能不在这些状态变化之间运行.因此,他们针对常规用例优化了该API,需要使用PendingIntent
,这样他们才能使您的应用再次运行以告诉您有关状态更改的信息.
The conventional use case for this API is to find out about changes in the device state over a period of time, and your process may not be running in between those changes of state. Hence, they optimized this API for the conventional use case, requiring a PendingIntent
so they can get your app running again to tell you about the state change.
如果您碰巧仅在您的一个魔术活动恰好在前台时才使用此API来了解状态变化,则可以在该活动上使用createPendingResult()
来获取将调用onActivityResult()
在同一活动上.但是,这是一个相当特殊的情况,尤其是对于此API.
If you happen to be using this API to find out about state changes only while one magic activity of yours happens to be in the foreground, you can use createPendingResult()
on that activity to get a PendingIntent
that will invoke onActivityResult()
on that same activity. However, this is a fairly niche scenario, particularly for this API.
否则,与使用AlarmManager
安排警报或使用JobScheduler
安排工作的情况一样,您需要以不同的方式处理此问题:
Otherwise, you need to treat this no differently than if you were scheduling an alarm with AlarmManager
or a job with JobScheduler
:
-
如果您要进行的工作少于1毫秒,请创建清单清单注册的
BroadcastReceiver
并使用getReceiver()
PendingIntent
如果您正在进行的工作需要1毫秒至15秒,请创建一个IntentService
并使用getService()
PendingIntent
If the work that you are doing will take between 1 millisecond and 15 seconds, create an IntentService
and use a getService()
PendingIntent
如果您所做的工作可能需要15秒钟以上,请创建清单清单注册的WakefulBroadcastReceiver
和和IntentService
,请使用getReceiver()
PendingIntent
,并让WakefulBroadcastReceiver
通过startWakefulService()
将工作传递给IntentService
.
If the work that you are doing may take in excess of 15 seconds, create a manifest-registered WakefulBroadcastReceiver
and an IntentService
, use a getReceiver()
PendingIntent
, and have the WakefulBroadcastReceiver
pass the work to the IntentService
via startWakefulService()
.
这篇关于使用/创建PendingIntent的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!