質問

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