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

Android AppUsageStatistics示例运行失败求助:已满足前置条件仍未解决

Troubleshooting the Android AppUsageStatistics Sample App Failure

I totally get the frustration when an official sample app doesn't run as expected—especially after checking all the prerequisites and making the required code changes. Let's walk through targeted troubleshooting steps based on your situation:

1. Verify Usage Stats Permission Setup (Critical!)

The PACKAGE_USAGE_STATS permission is a protected system permission that can't be granted automatically via standard runtime permissions. Here's what to double-check:

  • Ensure your AndroidManifest.xml includes this permission (with the tools ignore tag to bypass lint warnings):
    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
                     tools:ignore="ProtectedPermissions" />
    
  • Confirm the sample app triggers the permission settings intent. Look for code like this in AppUsageStatisticsFragment (add it if missing):
    private void requestUsageStatsPermission() {
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        startActivity(intent);
    }
    
  • Manually grant the permission on your test device: Go to Settings > Apps > [Your App Name] > Permission > Usage access, and toggle it on.

2. Fix Context/Activity Nullability in Your Code Change

You modified the line to get UsageStatsManager using getActivity(), but this can return null if called too early in the Fragment lifecycle. Replace it with the safer requireContext() method to avoid potential null pointer exceptions:

mUsageStatsManager = (UsageStatsManager) requireContext().getSystemService(Context.USAGE_STATS_SERVICE);

Make sure this line runs after the Fragment is attached to an Activity (e.g., in onViewCreated() or onActivityCreated(), not in onCreate()).

3. Validate Build Configuration Consistency

Even with the correct SDK and Build Tools installed, mismatched versions in your module's build.gradle can break the app:

  • Check that compileSdkVersion and targetSdkVersion are both set to 27.
  • Ensure your support library dependencies match SDK 27 (e.g., com.android.support:appcompat-v7:27.1.1).
  • Clean and rebuild your project (Build > Clean Project, then Build > Rebuild Project) to resolve cached build artifacts.

4. Rule Out Device/Emulator Quirks

  • Test on a physical Android 8.1 (API 27) device if you're using an emulator—emulators sometimes have permission-related bugs.
  • Uninstall the app completely, clear Android Studio's cache (File > Invalidate Caches... > Invalidate and Restart), then reinstall to eliminate stale data.

5. Diagnose with Logcat

Logcat is your best friend for pinpointing issues. Filter logs for keywords like UsageStatsManager, SecurityException, or NullPointerException:

  • A SecurityException confirms the app lacks the required usage stats permission.
  • A NullPointerException points to an uninitialized object (like mUsageStatsManager being null).

If you still hit a wall after these steps, share the relevant Logcat logs and any specific crash details, and we can dig deeper!

内容的提问来源于stack exchange,提问作者Marco Silvestri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:41:33