All Projects β†’ ricvalerio β†’ Foregroundappchecker

ricvalerio / Foregroundappchecker

Licence: apache-2.0
Foreground application detection library for android.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Foregroundappchecker

Awesome Android Ui
😎 A curated list of awesome Android UI/UX libraries
Stars: ✭ 353 (+111.38%)
Mutual labels:  library, android-development
K4kotlin
A sweet, small set of Kotlin functions to reduce your android boilerplate code
Stars: ✭ 210 (+25.75%)
Mutual labels:  library, android-development
Venom
A lightweight tool that simplifies testing of the process death scenario.
Stars: ✭ 218 (+30.54%)
Mutual labels:  library, android-development
Android Customtabs
Chrome CustomTabs for Android demystified. Simplifies development and provides higher level classes including fallback in case Chrome isn't available on device.
Stars: ✭ 378 (+126.35%)
Mutual labels:  library, android-development
Awesome Android
😎 A curated list of awesome Android resources
Stars: ✭ 26 (-84.43%)
Mutual labels:  library, android-development
Chocobar
The usual Snackbar with more 🍫 and colours πŸŽ‰
Stars: ✭ 110 (-34.13%)
Mutual labels:  library, android-development
Permissionsflow
A simple library to make it easy requesting permissions in Android using Kotlin Coroutines.
Stars: ✭ 49 (-70.66%)
Mutual labels:  library, android-development
Chucker
πŸ”Ž An HTTP inspector for Android & OkHTTP (like Charles but on device)
Stars: ✭ 2,169 (+1198.8%)
Mutual labels:  library, android-development
Aim Ik
A Unity-3D library, to procedural orientate character head (and chest) in a direction without using any animation data.
Stars: ✭ 164 (-1.8%)
Mutual labels:  library
Ssh2 Python
Bindings for libssh2 C library.
Stars: ✭ 166 (-0.6%)
Mutual labels:  library
Brackeys Ide
πŸ‘¨β€πŸ’» Brackeys IDE is a fast and free multi-language code editor for Android.
Stars: ✭ 154 (-7.78%)
Mutual labels:  library
Uriparser
πŸ”ͺ Strictly RFC 3986 compliant URI parsing and handling library written in C89; moved from SourceForge to GitHub
Stars: ✭ 163 (-2.4%)
Mutual labels:  library
Slidingsquareloaderview
Marvelous sliding square loader view
Stars: ✭ 166 (-0.6%)
Mutual labels:  library
Awesomedialog
A Beautiful Dialog Library for Kotlin Android
Stars: ✭ 163 (-2.4%)
Mutual labels:  android-development
Bt
BitTorrent library and client with DHT, magnet links, encryption and more
Stars: ✭ 2,011 (+1104.19%)
Mutual labels:  library
Pysnow
Python library for the ServiceNow REST API
Stars: ✭ 162 (-2.99%)
Mutual labels:  library
Blurimage
This Android Project help you to make your image blur in fastest way
Stars: ✭ 162 (-2.99%)
Mutual labels:  library
React Messenger
Chat UX components built with React, inspired by Facebook Messenger
Stars: ✭ 167 (+0%)
Mutual labels:  library
Cecil
Cecil is a library to inspect, modify and create .NET programs and libraries.
Stars: ✭ 2,112 (+1164.67%)
Mutual labels:  library
Angular Google Maps
Angular 2+ Google Maps Components
Stars: ✭ 1,982 (+1086.83%)
Mutual labels:  library

Foreground App Checker for android

This library tries to provide an easy way to get the foreground application package name. It uses different techniques, adequate to the device's android version.

Download

Download

apply plugin: 'com.android.application'

repositories {
    // ...
    maven { url 'https://dl.bintray.com/rvalerio/maven' }
}

dependencies {
    // ...
    compile 'com.rvalerio:fgchecker:1.1.0'
}

Setup

To use this library, you will need to request permissions in order to support different versions.

To support ICS until Lollipop, you need to request android.permission.GET_TASKS permission.

To support Lollipop or above, you need to request android.permission.PACKAGE_USAGE_STATS permission.

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

For your convenience, I provide here code to request usage stats permission:

    
void requestUsageStatsPermission() {
    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP 
        && !hasUsageStatsPermission(this)) {
        startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
    }
}

@TargetApi(Build.VERSION_CODES.KITKAT)
boolean hasUsageStatsPermission(Context context) {
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow("android:get_usage_stats",
            android.os.Process.myUid(), context.getPackageName());
    boolean granted = mode == AppOpsManager.MODE_ALLOWED;
    return granted;
}

Usage

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);

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);

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);

Callbacks are done on the UI thread. Keep in mind that callbacks are done every time there is a scan. Currently it does not do callbacks only when the foreground app changes.

Upcoming features

  • Add option to only callback when there is a package name change

This library is distributed under the Apache 2.0 license.

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].