I just want to get numbers of times to be used for each Activity. So the very straightforward method I thought is increasing the count for an Activity when it was started. B
If you're an Android OEM developer/partner, you can use IActivityController.Stub to get what you need. You can then use ActivityManagerNative as below to start receiving events.
ActivityManagerNative.getDefault().setActivityController(activityController);
You would need to save your whitelist/blacklist preferences somewhere and compare with the package from the IActivityController.Stub callback. E.g.,
public boolean activityStarting(Intent intent, String pkg) {
Slog.d(AdbLogTag.TAG, "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Slog.i(AdbLogTag.TAG, String.format("Application starting {intent=%s}, pkg=%s", intent, pkg));
Slog.d(AdbLogTag.TAG, "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Hope this helps.