UsageEvents崩溃了我的应用程序

时间:2017-01-12 19:59:56

标签: android android-asynctask

我的应用程序崩溃在这里我不知道为什么,但它只发生在Android 5.1和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;

    }

}

这是它崩溃的地方

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:包已经完成!                                                                          在android.os.BinderProxy.transactNative(原生方法)                                                                          在android.os.BinderProxy.transact(Binder.java:496)                                                                          在android.app.usage.IUsageStatsManager $ Stub $ Proxy.queryEvents(IUsageStatsManager.java:194)                                                                          在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)                                                                          在android.os.AsyncTask $ 2.call(AsyncTask.java:292)                                                                          在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                          在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)                                                                          在java.lang.Thread.run(Thread.java:818)   01-12 13:44:12.572 29626-29688 / ca.dti.grounded.app A / libc:致命信号11(SIGSEGV),代码1,故障地址0x10 in tid 29688(AsyncTask#2)

1 个答案:

答案 0 :(得分:-1)

我有同样的问题......似乎中断; 从字面上打破了Android 5.1.1 API。

故事的寓意是:

  

如果您曾致电 queryEvents ,请确保从队列中调用所有 getNextEvent 。如果不这样做,应用程序将在接下来的几次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;
            }
        }
    }