From 2801364ed875b34412341a48f11a81aab593cd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Thu, 2 Feb 2017 13:05:43 +0100 Subject: [PATCH] Avoid reflections --- src/android/BackgroundExt.java | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/android/BackgroundExt.java b/src/android/BackgroundExt.java index bf2026a..69e4ec6 100644 --- a/src/android/BackgroundExt.java +++ b/src/android/BackgroundExt.java @@ -21,8 +21,10 @@ package de.appplant.cordova.plugin.background; +import android.annotation.TargetApi; import android.app.Activity; import android.app.ActivityManager; +import android.app.ActivityManager.AppTask; import android.content.Context; import android.content.Intent; import android.os.Build; @@ -148,6 +150,7 @@ class BackgroundExt { /** * Exclude the app from the recent tasks list. */ + @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void excludeFromTaskList() { ActivityManager am = (ActivityManager) getActivity() .getSystemService(Context.ACTIVITY_SERVICE); @@ -155,21 +158,12 @@ class BackgroundExt { if (am == null || Build.VERSION.SDK_INT < 21) return; - try { - Method getAppTasks = am.getClass().getMethod("getAppTasks"); - List tasks = (List) getAppTasks.invoke(am); + List tasks = am.getAppTasks(); - if (tasks == null || tasks.isEmpty()) - return; + if (tasks == null || tasks.isEmpty()) + return; - ActivityManager.AppTask task = (ActivityManager.AppTask) tasks.get(0); - Method setExcludeFromRecents = task.getClass() - .getMethod("setExcludeFromRecents", boolean.class); - - setExcludeFromRecents.invoke(task, true); - } catch (Exception e) { - e.printStackTrace(); - } + tasks.get(0).setExcludeFromRecents(true); } /**