首次安装后WazeApp无法触发启动的问题修复请求
Hey there! Let's break down why your app isn't triggering Waze properly right after first installation, and fix it step by step.
Common Causes & Fixes
1. Missing Usage Access Permission
The biggest culprit here is likely that your app doesn't have the Usage Access Permission enabled yet. On Android, apps need explicit user approval to access usage stats via UsageStatsManager, and this permission isn't granted automatically on install.
What to do:
- First, add the permission to your
AndroidManifest.xml:<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" /> - Then, in your code, check if the permission is granted before calling
wasWazeRunningBefore5Minutes. If not, redirect the user to the system settings to enable it:private boolean hasUsageStatsPermission(Context context) { AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName()); return mode == AppOpsManager.MODE_ALLOWED; } // Call this before your trigger logic if (!hasUsageStatsPermission(context)) { Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); context.startActivity(intent); // Show a toast to explain why this permission is needed Toast.makeText(context, "Enable usage access to let the app check Waze activity", Toast.LENGTH_LONG).show(); return; }
2. No Usage Stats for Waze on First Install
When you first install your app, the system might not have any usage records for Waze yet (either because Waze hasn't been opened since your app was installed, or the system hasn't logged the data). Your current code returns false in this case, meaning it won't trigger Waze—but this is probably not what you want.
Fix the logic:
Modify your wasWazeRunningBefore5Minutes function to return true if Waze's usage stats are not found. This way, if there's no record of Waze being used recently, the app will assume it's been inactive for over 5 minutes and launch it:
private static final String WAZE_PACKAGE_NAME = "com.waze"; // Verify this is correct! private static final int TIME_INTERVAL = 5 * 60; // 300 seconds private static boolean wasWazeRunningBefore5Minutes(Context context) { long current = System.currentTimeMillis(); long fiveMinuteBeforeMillis = current - TimeUnit.SECONDS.toMillis(TIME_INTERVAL); UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); List<UsageStats> stats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, fiveMinuteBeforeMillis, current); boolean foundWazeStats = false; for (UsageStats stat : stats) { if (stat.getPackageName().equals(WAZE_PACKAGE_NAME)) { foundWazeStats = true; long lastTimeUsed = stat.getLastTimeUsed(); Log.d(TAG, "last time used: " + DateFormat.format("dd/MM/yyyy, hh:mm:ss", new Date(lastTimeUsed))); long gap = TimeUnit.MILLISECONDS.toSeconds(current - lastTimeUsed); Log.d(TAG, "app open gap: " + gap + " sec"); // Return true if gap exceeds 5 minutes return gap > TIME_INTERVAL; } } // If no Waze stats found, assume it's been inactive for over 5 minutes return !foundWazeStats; }
3. Double-Check Waze Package Name
Make sure WAZE_PACKAGE_NAME is set to the correct value: com.waze. A typo here would mean your code never finds Waze in the usage stats list.
Testing Steps
- Install your app for the first time.
- When prompted, enable the Usage Access permission in settings.
- Test the trigger logic—if Waze hasn't been used recently, it should now launch correctly.
内容的提问来源于stack exchange,提问作者VIKAS SHARMA

