# foregroundappchecker **Repository Path**: HarmonyOS-tpc/foregroundappchecker ## Basic Information - **Project Name**: foregroundappchecker - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-17 - **Last Updated**: 2023-04-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Foreground App Checker ## Introduction This library is used to provide an easy way to get the foreground application package name. ### Features Allow to get the foreground application package name. Allow to get the foreground application package name on an interval. Allow to get callback for specific package name when it is in foreground. ## Usage Instructions Step 1. Add permission “ohos.permission.BUNDLE_ACTIVE_INFO” in config.json file. Step 2. Enable the permission explicitly by launching the below setting ability. Intent intent = new Intent(); intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT); Operation operation = new Intent.OperationBuilder() .withBundleName(“com.android.settings”) .withAbilityName(“com.android.settings.Settings$UsageAccessSettingsActivity”) .build(); intent.setOperation(operation); startAbility(intent); Step 3.1 In it's simplest form, and to get the package name of the foreground application, you can do like so: AppChecker appChecker = new AppChecker(); String packageName = appChecker.getForegroundApp(context); Step 3.2 If you would like to have this being checked on an interval, you can do like so: AppChecker appChecker = new AppChecker(); appChecker .whenAny(new AppChecker.Listener() { @Override public void onForeground(String packageName) { // do something } ) .timeout(1000) .start(this); Step 3.3 If you want to check for specific package names, it also provides a way to do so: AppChecker appChecker = new AppChecker(); appChecker .when("com.other.app", new AppChecker.Listener() { @Override public void onForeground(String packageName) { // do something when com.other.app is in the foreground } ) .when("com.my.app", new AppChecker.Listener() { @Override public void onForeground(String packageName) { // do something when com.my.app is in the foreground } ) .whenOther(new AppChecker.Listener() { @Override public void onForeground(String packageName) { // do something when none of the registered packages are in the foreground } ) .whenAny(new AppChecker.Listener() { @Override public void onForeground(String packageName) { // do something everytime a scan for foreground app is run } ) .timeout(1000) .start(this); ## Installation tutorial Solution 1: local har package integration 1. Add the .har package to the lib folder. 2. Add the following code to the gradle of the entry: dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) Solution 2: Add following dependencies in your build.gradle: dependencies { implementation project(':fgchecker') } Solution 3: For using foregroundappchecker from a remote repository in separate application, add the below dependency in entry/build.gradle file. Modify entry build.gradle as below : ```gradle dependencies { implementation 'io.openharmony.tpc.thirdlib:foregroundappchecker:1.0.0' } ``` License ------- Copyright 2016 Ricardo Valério Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.