How to keep a foreground app running 24/7?

前端 未结 7 630
时光说笑
时光说笑 2021-01-30 18:25

I am looking into how to keep my Android app running in the foreground.

It will be a privately distributed app, so I can do anything possible to make sure it runs consta

7条回答
  •  天涯浪人
    2021-01-30 19:06

    Now that activityManager.getRunningAppProcesses() is deprecated( as of API21 ), you will want to replace :

    RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
    String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
    

    with:

    List tasks = activityManager.getRunningAppProcesses();
    String foregroundTaskPackageNameTest = tasks.get(0).processName;
    

    do not forget to import List with:

    import java.util.List;
    

    As a side note, I am not sure about OP's way of keeping the screen always on. I'm not sure that it works the way he's done it, but more importantly, it is deprecated and very much advised against to be using Wake Locks as you need to add permissions, which opens the door to bugs. Rather, it is generally better practice to use Window manager flags: https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON

提交回复
热议问题