I am trying to make a kiosk type app for an android device, and I want to ensure that the app is always running, and restart it if it falls over.

My initial thought was to create a Service, pinned to the foreground, which periodically checks (using the system Handler) to see if the Activity (the main App with the UI) is running. If it isn't running, then the Service should restart the Activity by launching an intent using:

Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("com.blah.blah.MyApp"));
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);

Is it possible to see if a specific app is running or not? And for bonus internet-points - could I tell if that app is in the foreground (and use the Service to keep the Activity in the foreground)?

有帮助吗?

解决方案

You can check the current Application(any application) package name running at the top String packageName= getSystemService(Context.ACTIVITY_SERVICE).getRunningTasks(1).get(0)).topActivity.getPackageName();

Run this code in your background service in a loop and checks for your application.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top