当前位置: 首页 > 知识库问答 >
问题:

UsageEvents使我的应用程序崩溃

施慈
2023-03-14
   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(时间-100*1000,时间);event=new UsageEvents.event();

01-12 13:44:12.571 296 26-29688/ca.dti.grounded.app E/OtherService:AsynchChecker 287:73 java.lang.IllegalStateException:Parcel已完成!在android.os.binderproxy.transactNative(本机方法)在android.os.binderproxy.transact(binder.java:496)在android.app.usage.iusageStatsManager.java:194)在android.app.usage.usageStatsManager.queryevents(usageStatsManager.java:180)在ca.dti.grounded.app.services.dataandsocialservice$asynchchecker.doinbackground(dataandsocialservice:286)在ca.dti.grounded.app.services.dataandsocialservice$asynchchecker.doin.后台(DataAndSocialService.java:260)在Android.os.AsyncTask$2。调用(Asynctask.java:292)在java.util.concurrent.FutureTask.run(FutureTask.java:237)在Android.os.AsyncTask$SerialExecutor$1。运行(Asynctask.java:231)在java.util.concurrent.ThreadPoolExecutor.RunWorker(ThreadPoolExecutor.1112)在java.util.concurrent.ThreadPoolExecutor$Worker.Run(ThreadPoolExecutor.587)在java.lang.Thread.Run(Thread.java:818)01-12 13:15 44:12.572 296 26-29688/ca.dti.grounded.app A/libc:致命信号11(SIGSEGV),代码1,故障地址0x10 intid 29688(异步任务#2)

共有1个答案

周云
2023-03-14

我也有同样的问题...似乎断了;实际上破坏了Android5.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;
            }
        }
    }
 类似资料:
  • 注意:在我添加广告之前,我的应用程序运行良好,使用相同的方法 当我试图将adview添加到我的应用程序时,它会使我的应用程序崩溃,所以我删除了它,但仍然给我这个这是logcat 10-12 21:33:19.765 4993-4993/com。fm360。almorfis E/AndroidRuntime:致命异常:主进程:com。fm360。阿尔莫菲斯,PID:4993爪哇。lang.Runti

  • 更新到QRCodeReaderView最新版本后,我的应用程序崩溃仍然没有停止,不同的问题得到。致命异常:java.lang.RuntimeException:无法连接到java:580)在android.view.choreographer.doFrame(choreographer.java:550)在android.view.choreographer$FrameDisplayEventRe

  • 我创建了一个应用程序,在Android marshmallow中崩溃,而在under版本中我的应用程序正常工作。 这怎么可能?这是我的清单代码: 这是分级代码: 我读到你必须在代码中修改一些东西,使其与AndroidMarshmallow兼容。 我该如何解决问题呢?

  • 我正在尝试将图像添加到我新创建的应用程序中。但是,当我运行我的应用程序时,它会崩溃。我没有在我的应用程序中编写任何代码,因此我提供了XML代码和错误日志。我在以前的应用程序中使用了ImageViews。没有错误。我已经优化了我的图像,但它仍然给我错误。 图像详细信息 错误日志: 09-08 16:03:02.266 15137-15137/ooper.loopE/AndroidRuntime: F

  • 当我运行应用程序并转到用户的活动时,应用程序崩溃,显示mUsersList。setHasFixedSize(真);正在使应用程序崩溃。 这是消息“由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void androidx.recyclerview.widget.recyclerview.setHasFixedSize(布尔)'”

  • 我正在使用SharedPref在活动之间传递字符串。我正试图使我的代码更有效,但我有一个我无法解决的问题。 MainActivity.Class中的我的代码: @override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(r.layout.