1

My Application crashes here i dont know why but it only happened in Android 5.1 and 5.0.1

   public class AsynchChecker extends AsyncTask<Void, Void, Boolean> {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected Boolean doInBackground(Void... params) {


        /***********************************************************/

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                try {
                    UsageStatsManager usageStatsManager = (UsageStatsManager) getApplicationContext()
                            .getSystemService(Context.USAGE_STATS_SERVICE);//"usagestats"
                    long milliSecs = 60 * 1000;
                    Date date = new Date();
                    List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(
                            UsageStatsManager.INTERVAL_DAILY, date.getTime() - milliSecs, date.getTime());
                    if (queryUsageStats.size() > 0) {
                        Log.i("LPU", "queryUsageStats size: " + queryUsageStats.size());
                    }
                    // long recentTime = 0;
                    long time = System.currentTimeMillis();
                    Log.wtf("time",time + " " +Calendar.getInstance().getTime() + " ");
                    UsageEvents usageEvents = null;
                    UsageEvents.Event event = null;
                    try {
                        usageEvents = usageStatsManager.queryEvents(time - 100 * 1000, time);
                        event = new UsageEvents.Event();
                    } catch (Exception ex) {
                        Log.e("OtherService ", "AsynchChecker 287:73", ex);
                        stopSelf();
                        startService(new Intent(DataAndSocialService.this, OptionsServices.class));
                        return false;
                    } catch (Throwable throwable) {
                        Log.e("OtherService ", "AsynchChecker 289:73", throwable);
                        stopSelf();
                        startService(new Intent(DataAndSocialService.this, OptionsServices.class));
                        return false;
                    }
                    ArrayList<UsageStats> recentPkg = new ArrayList<UsageStats>();
                    for (int i = 0; i < queryUsageStats.size(); i++) {
                        UsageStats stats = queryUsageStats.get(i);
                        // Log.i("LPU", "PackageName: " +
                        // stats.getPackageName() + " " +
                        // stats.getLastTimeStamp());
                        while (usageEvents.hasNextEvent()) {
                            usageEvents.getNextEvent(event);
                        }
                        if (stats.getPackageName().equals(event.getPackageName())
                                && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
                            Log.i("Foregroung", "PackageName: " + stats.getPackageName() + " Foreground");

                            /**
                             * Check if the running application is
                             * restrected
                             */
                            if ((stats.getPackageName().equals("com.android.settings") || stats.getPackageName()
                                    .equals("com.android.settings.Settings$DataUsageSummaryActivity")) && start_data
                                    && checkTime() && !isDetected) {

                                isDetected = true;
                                this.cancel(true);
                                getBaseContext()
                                        .startActivity(new Intent(getBaseContext(), LockerAuthentification.class)
                                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                                        | Intent.FLAG_ACTIVITY_NO_USER_ACTION));
                            } else {
                                isDetected = false;
                            }

                            if ((stats.getPackageName().equals("com.android.mms")
                                    || stats.getPackageName().equals("com.google.android.mms")
                                    || stats.getPackageName().equals("com.google.android.talk")
                                    || stats.getPackageName().equals("com.android.talk")
                                    || stats.getPackageName().equals("com.android.vending")
                                    || stats.getPackageName().equals("com.facebook.katana")
                                    || stats.getPackageName().equals("com.facebook.orca")
                                    || stats.getPackageName().equals("com.whatsapp")
                                    || stats.getPackageName().equals("com.snapchat.android")
                                    || stats.getPackageName().equals("com.instagram.android")
                                    || stats.getPackageName().equals("co.vine.android")
                                    || stats.getPackageName().equals("kik.android")
                                    || stats.getPackageName().equals("com.skype.raider")) && start_texting
                                    && checkTime() && !isDetected) {
                                isDetected = true;
                                this.cancel(true);
                                getBaseContext().startActivity(new Intent(getBaseContext(), LockerAuthentification.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION));
                            } else {
                                isDetected = false;
                            }

                        }

                        recentPkg.add(stats);
                    }
                } catch (Exception e) {
                    Log.e("OtherService ", "AsynchChecker 337:72", e);
                    stopSelf();
                    startService(new Intent(DataAndSocialService.this, OptionsServices.class));
                } catch (Throwable throwable) {
                    Log.e("OtherService ", "AsynchChecker 353:69", throwable);
                    stopSelf();
                    startService(new Intent(DataAndSocialService.this, OptionsServices.class));
                }

            } else {
                /**
                 * Get AtivityManager that is responsible for the activies
                 * interactions running on the system android
                 */
                ActivityManager am = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
                /** Get List get running applications */
                List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
                // Log.e("isChecked", start + "");
                /***********************************************************/
                /** Loop over all the running applications */
                for (int i = 0; i < runningAppProcessInfo.size(); i++) {

                    // strName.get(0).

                    /** Check if the running application is restrected */
                    if ((runningAppProcessInfo.get(i).processName.equals("com.android.settings")
                            || runningAppProcessInfo.get(i).processName
                            .equals("com.android.settings.Settings$DataUsageSummaryActivity"))
                            && checkTime()) {
                        // Log.e("process",
                        // runningAppProcessInfo.get(i).processName);

                        if (runningAppProcessInfo.get(i).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                                && !isDetected) {
                            Log.e("foreground", runningAppProcessInfo.get(i).processName);
                            isDetected = true;
                            this.cancel(true);
                            getBaseContext().startActivity(
                                    new Intent(getBaseContext(), LockerAuthentification.class).addFlags(
                                            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION));
                        } else {
                            isDetected = false;
                        }
                    }

                    if ((runningAppProcessInfo.get(i).processName.equals("com.android.mms")
                            || runningAppProcessInfo.get(i).processName.equals("com.google.android.mms")
                            || runningAppProcessInfo.get(i).processName.equals("com.google.android.talk")
                            || runningAppProcessInfo.get(i).processName.equals("com.android.talk")
                            || runningAppProcessInfo.get(i).processName.equals("com.android.vending")
                            || runningAppProcessInfo.get(i).processName.equals("com.facebook.katana")
                            || runningAppProcessInfo.get(i).processName.equals("com.facebook.orca")
                            || runningAppProcessInfo.get(i).processName.equals("com.whatsapp")
                            || runningAppProcessInfo.get(i).processName.equals("com.snapchat.android")
                            || runningAppProcessInfo.get(i).processName.equals("com.instagram.android")
                            || runningAppProcessInfo.get(i).processName.equals("co.vine.android")
                            || runningAppProcessInfo.get(i).processName.equals("kik.android")
                            || runningAppProcessInfo.get(i).processName.equals("com.skype.raider")) && start_texting
                            && checkTime()) {
                        // Log.e("process",
                        // runningAppProcessInfo.get(i).processName);

                        if (runningAppProcessInfo.get(i).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                                && !isDetected) {
                            Log.e("foreground", runningAppProcessInfo.get(i).processName);
                            isDetected = true;
                            this.cancel(true);
                            getBaseContext().startActivity(new Intent(getBaseContext(), LockerAuthentification.class).addFlags(
                                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION));
                        } else {
                            isDetected = false;
                        }

                    }

                }

            }
        } catch (Exception e) {
            Log.e("OtherService ", "AsynchChecker 411:61", e);
        } catch (Throwable throwable) {
            Log.e("OtherService ", "AsynchChecker 430:61", throwable);
        }
        return true;

    }

}

this is where it crashes

usageEvents = usageStatsManager.queryEvents(time - 100 * 1000, time); event = new UsageEvents.Event();

01-12 13:44:12.571 29626-29688/ca.dti.grounded.app E/OtherService: AsynchChecker 287:73 java.lang.IllegalStateException: Parcel has been finalized! at android.os.BinderProxy.transactNative(Native Method) at android.os.BinderProxy.transact(Binder.java:496) at android.app.usage.IUsageStatsManager$Stub$Proxy.queryEvents(IUsageStatsManager.java:194) at android.app.usage.UsageStatsManager.queryEvents(UsageStatsManager.java:180) at ca.dti.grounded.app.services.DataAndSocialService$AsynchChecker.doInBackground(DataAndSocialService.java:286) at ca.dti.grounded.app.services.DataAndSocialService$AsynchChecker.doInBackground(DataAndSocialService.java:260) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) 01-12 13:44:12.572 29626-29688/ca.dti.grounded.app A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x10 in tid 29688 (AsyncTask #2)

Hicham Bagui
  • 240
  • 1
  • 17
  • `java.lang.IllegalStateException: Parcel has been finalized` seems to indicate a state problem with your call to `queryEvents()`. Make sure your `usageStatsManager` is in a right state when calling `queryEvents()`. Hard to say without seeing more of your code. – Distwo Jan 12 '17 at 20:05
  • i just did an update @D – Hicham Bagui Jan 12 '17 at 20:22
  • Looks like your issue is similar to this one: http://stackoverflow.com/questions/32774571/android-permission-package-usage-stats-only-for-system-apps – Distwo Jan 12 '17 at 21:17
  • it work for 6.0 and plus i dont think its the case – Hicham Bagui Jan 12 '17 at 21:19
  • Anything new on this? I'm facing the same issue. App works fine on Android 6, but crashes on 5.x – battlepope Jun 28 '17 at 13:09
  • No its common issue with Android that they didnt fix at all i had to move to Android 6 and reduce the functionality on Android 5 – Hicham Bagui Jun 29 '17 at 14:05

1 Answers1

-1

I had same issue... It seems that break; literally breaks Android 5.1.1 API.

The moral of the story is:

If you ever call queryEvents then make sure you call all getNextEvent from the queue. If you don't, app will crash in next few runs of queryEvents.

    UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Service.USAGE_STATS_SERVICE);
    long time = System.currentTimeMillis();

    UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 3600, time);
    UsageEvents.Event event = new UsageEvents.Event();
    while (usageEvents.hasNextEvent()) {
        usageEvents.getNextEvent(event);
        if(event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            if (event.getPackageName().equals(appName)) {
                isAppRunning = true;
                //break;
            }
        }
    }
mzmrk
  • 109
  • 1
  • 4
  • The code in the question doesn't actually use `break;` when looping through the results of `queryEvents()`. I think your error is a different issue. – Sam Feb 17 '19 at 03:07
  • @Sam It is same issue. If in code in main question queryUsageStats.size() is equal to 0. Then code will never get to part that takes all the elements from usageEvents. – mzmrk Feb 28 '19 at 11:06