简体   繁体   English

活动转换API请求一次活动转换更新

[英]Activity Transition API request activity-transition-updates once

I am trying to implement activity transition updates in the background of my application. 我正在尝试在应用程序的后台实施活动转换更新。 My goal is to request activity transition updates once and get activity transition updates all the time in the background. 我的目标是一次请求活动转换更新,并始终在后台获取活动转换更新。 To achieve this, I implemented the following: 为此,我实现了以下内容:

PendingIntent pi = PendingIntent.getBroadcast(mApplicationContext, 100, intent,
                PendingIntent.FLAG_NO_CREATE);
        if (pi == null) {
            pi = PendingIntent.getBroadcast(mApplicationContext, 100,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Task<Void> task = ActivityRecognition.getClient(mApplicationContext)
                    .requestActivityTransitionUpdates(atr, pi);
            task.addOnCompleteListener(task1 -> {
                if (task1.isSuccessful()) {
                    Log.v(TAG, "Travel behavior activity-transition-update set up");
                } else {
                    Log.v(TAG, "Travel behavior activity-transition-update failed set up: " +
                            task1.getException().getMessage());
                    task1.getException().printStackTrace();
                }
            });
        }

So, in this code if PendingIntent.getBroadcast(mApplicationContext, 100, intent,PendingIntent.FLAG_NO_CREATE); 因此,在此代码中,如果PendingIntent.getBroadcast(mApplicationContext, 100, intent,PendingIntent.FLAG_NO_CREATE); returns null, I call requestActivityTransitionUpdates method. 返回null,我调用requestActivityTransitionUpdates方法。 Otherwise, I don't request, because the activity transition updates are already been requested with the pending intent. 否则,我不要求,因为已经请求了活动转换更新以及未决的意图。

However, this code doesn't work. 但是,此代码不起作用。 After the first requestActivityTransitionUpdates , PendingIntent.getBroadcast(mApplicationContext, 100, intent, PendingIntent.FLAG_NO_CREATE) returns a pending intent and I don't call requestActivityTransitionUpdates and I stop getting transition updates. 在第一个requestActivityTransitionUpdatesPendingIntent.getBroadcast(mApplicationContext, 100, intent, PendingIntent.FLAG_NO_CREATE)返回一个挂起的意图,我不调用requestActivityTransitionUpdates ,而是停止获取过渡更新。

If I use the following code by removing the PendingIntent.getBroadcast(mApplicationContext, 100, intent, PendingIntent.FLAG_NO_CREATE) line: 如果我通过删除PendingIntent.getBroadcast(mApplicationContext, 100, intent, PendingIntent.FLAG_NO_CREATE)行使用以下代码:

PendingIntent pi = PendingIntent.getBroadcast(mApplicationContext, 100,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Task<Void> task = ActivityRecognition.getClient(mApplicationContext)
                    .requestActivityTransitionUpdates(atr, pi);
            task.addOnCompleteListener(task1 -> {
                if (task1.isSuccessful()) {
                    Log.v(TAG, "Travel behavior activity-transition-update set up");
                } else {
                    Log.v(TAG, "Travel behavior activity-transition-update failed set up: " +
                            task1.getException().getMessage());
                    task1.getException().printStackTrace();
                }
            });

I keep getting the transition updates. 我一直在获取过渡更新。 But in this way, I have to request transition updates every time when the app opens. 但是以这种方式,每次应用程序打开时,我都必须请求过渡更新。 And after each transition update request, the API returns the current activity in my BroadcastReceiver class even though there is no actual activity transition happened. 并且在每个转换更新请求之后,即使没有实际的活动转换发生,API也会在我的BroadcastReceiver类中返回当前活动。

So, is there a way to request transition updates once and keep getting transition updates all the time? 因此,是否有一种方法可以请求一次过渡更新并始终保持过渡更新?

You can use the second way. 您可以使用第二种方法。

But as you say the broadcast receiver will be notified at each app run because the transition API returns the last detected activity. 但是正如您所说,广播接收器将在每次应用运行时收到通知,因为过渡API返回了最后检测到的活动。

You can get that event time and ignore it if it's an old one or if it's repeated by saving the last received one in some where such as db or shared preferences. 您可以获取该事件时间,如果它是旧事件,或者如果它是重复发生的,则可以忽略它,方法是将最近接收到的事件保存在某些地方,例如db或共享首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM