문제

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