You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Tapresearch SDK的onSurveyModalClosed监听失效,如何检测SurveyActivity是否结束?

Got it, let's figure out how to detect when TapResearch's SurveyActivity finishes since their built-in onSurveyModalClosed() listener isn't working for you. Here are a few solid approaches that don't rely on that broken callback:

1. Global Activity Lifecycle Listener (Most Reliable)

This method works regardless of how TapResearch launches the SurveyActivity—it listens for every activity's lifecycle events across your app.

Implementation Steps:

  • Add a lifecycle callback to your app's Application class (or your main activity, if you prefer):
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            // Leave unused lifecycle methods empty
            @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}
            @Override public void onActivityStarted(@NonNull Activity activity) {}
            @Override public void onActivityResumed(@NonNull Activity activity) {}
            @Override public void onActivityPaused(@NonNull Activity activity) {}
            @Override public void onActivityStopped(@NonNull Activity activity) {}
            @Override public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {}

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                // Check if the destroyed activity is TapResearch's SurveyActivity
                if ("com.tapr.internal.activities.survey.SurveyActivity".equals(activity.getClass().getName())) {
                    // Run your app-resume logic here
                    resumeAppFunctionality();
                }
            }
        });
    }

    private void resumeAppFunctionality() {
        // Example: Resume a paused game, refresh UI state, or re-enable user interactions
        // If you need to trigger this in a specific activity, use LiveData or LocalBroadcastManager to send a signal
    }
}
  • Don't forget to declare your custom Application class in the AndroidManifest.xml:
<application
    android:name=".MyApp"
    <!-- other attributes -->
>

Pro Tip:

If you need to trigger resume logic in a specific activity (not the application class), use a LiveData object or LocalBroadcastManager to send a notification that the survey has closed.

2. Activity Result API (If You Control the Launch)

If TapResearch's SDK lets you retrieve the Intent used to launch SurveyActivity, you can use Android's modern Activity Result API to listen for when it finishes.

Implementation Steps:

public class MainActivity extends AppCompatActivity {
    private ActivityResultLauncher<Intent> surveyLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize the launcher
        surveyLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                // This fires when SurveyActivity finishes (regardless of result code)
                resumeApp();
            }
        );

        // When you're ready to launch the survey (hypothetical SDK method)
        Intent surveyIntent = TapResearch.getSurveyLaunchIntent(this);
        surveyLauncher.launch(surveyIntent);
    }

    private void resumeApp() {
        // Your resume logic here
    }
}

Note:

This only works if you can explicitly launch the survey via an intent from your code. If TapResearch's SDK launches the activity internally (e.g., via a service or broadcast), this method won't work.

3. Accessibility Service (Last Resort)

This method requires user authorization to enable accessibility features, so it's not ideal for most apps—but it's a fallback if the above options fail.

Implementation Steps:

  1. Create an Accessibility Service class:
public class SurveyCloseDetectorService extends AccessibilityService {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            String packageName = event.getPackageName().toString();
            String className = event.getClassName().toString();

            // Check if the SurveyActivity window is closing
            if ("com.tapresearch.sdk".equals(packageName) && 
                "com.tapr.internal.activities.survey.SurveyActivity".equals(className) &&
                !event.isEnabled()) {
                // Send a broadcast to notify your app the survey closed
                Intent closeIntent = new Intent("SURVEY_CLOSED_ACTION");
                sendBroadcast(closeIntent);
            }
        }
    }

    @Override
    public void onInterrupt() {}
}
  1. Register the service in your AndroidManifest.xml:
<service
    android:name=".SurveyCloseDetectorService"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_config" />
</service>
  1. Create res/xml/accessibility_config.xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true"
    android:description="@string/accessibility_service_desc" />
  1. In your activity, register a broadcast receiver to listen for the survey close event.

内容的提问来源于stack exchange,提问作者Fat-Bee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:56:37