如何保持前台应用全天候运行24/7? [英] How to keep a foreground app running 24/7?
问题描述
我正在研究如何使我的Android应用程序在前台运行.
I am looking into how to keep my Android app running in the foreground.
这将是一个私人分发的应用程序,因此我可以做任何事情来确保它在设备(HDMI TV Stick)上持续运行
It will be a privately distributed app, so I can do anything possible to make sure it runs constantly on the device (HDMI TV Stick)
那么,无论如何,我如何确保该应用程序保持运行?就资源使用而言,该应用程序的重量很轻,因此希望它以24/7的速度运行应该不会有问题.
So, how can I make sure that the app stays running no matter what? The app is pretty light weight in terms of resource usage, so having it run 24/7 should hopefully not be a problem.
我了解了清单中的持久参数,但看起来它可能仅适用于系统应用程序?
I read about the persistent parameter in the manifest, but it looks like it might only apply to system apps?
我应该将我的应用程序设置为系统应用程序吗?我该怎么办,对您有帮助吗?
Should I make my app a system app? How would I do that and would it help?
推荐答案
-
如果要使用外部应用程序:自动启动和StaY!
如果要以编程方式执行此操作,则可以使用一项服务,该服务每隔"x"毫秒轮询一次,以查看您的应用程序是否位于前台.如果不是,它将在前台启动/启用您的应用程序.这样做:
If you want to do this programmatically you can use a service that polls every "x" milliseconds to see if your app is in the foreground. If it is not, it will start/bring your app in the foreground. Do it like this:
public class PersistService extends Service { private static final int INTERVAL = 3000; // poll every 3 secs private static final string YOUR_APP_PACKAGE_NAME = "YOUR_APP_PACKAGE_NAME"; private static boolean stopTask; private PowerManager.WakeLock mWakeLock; @Override public void onCreate() { super.onCreate(); stopTask = false; // Optional: Screen Always On Mode! // Screen will never switch off this way mWakeLock = null; if (settings.pmode_scrn_on){ PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "a_tag"); mWakeLock.acquire(); } // Start your (polling) task TimerTask task = new TimerTask() { @Override public void run() { // If you wish to stop the task/polling if (stopTask){ this.cancel(); } // The first in the list of RunningTasks is always the foreground task. RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0); String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName(); // Check foreground app: If it is not in the foreground... bring it! if (!foregroundTaskPackageName.equals(YOUR_APP_PACKAGE_NAME)){ Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(YOUR_APP_PACKAGE_NAME); startActivity(LaunchIntent); } } }; Timer timer = new Timer(); timer.scheduleAtFixedRate(task, 0, INTERVAL); } @Override public void onDestroy(){ stopTask = true; if (mWakeLock != null) mWakeLock.release(); super.onDestroy(); } }
上面的代码还具有选项",以强制屏幕始终保持打开状态!当然,您将需要以下权限:
The above code has also the "option" to force the Screen to stay always on! Of course you will need the following permissions:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
也不要忘记注册您的服务:
and do not also forget to register your service:
<service android:name="YOURPACAKGE.PersistService"
android:enabled="true"/>
这篇关于如何保持前台应用全天候运行24/7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!