How to use service to get the information of which app is opened?How to `getTopActivity` name or get currently running application package name in lollipop?How to check if a service is running on Android?Is quitting an application frowned upon?How can I open a URL in Android's web browser from my application?Start an Activity with a parameterHow do I get the APK of an installed app without root access?Can't start Eclipse - Java was started but returned exit code=13What are the Android SDK build-tools, platform-tools and tools? And which version should be used?Mipmap drawables for iconsHow to make app lock app in android?Outdated Kotlin Runtime warning in Android Studio
Why don't modern jet engines use forced exhaust mixing?
A Magic Diamond
What's the point of writing that I know will never be used or read?
Why can't I see 1861 / 1871 census entries on Freecen website when I can see them on Ancestry website?
Have made several mistakes during the course of my PhD. Can't help but feel resentment. Can I get some advice about how to move forward?
Why should I pay for an SSL certificate?
What was the intention with the Commodore 128?
How to render "have ideas above his station" into German
Why was ramjet fuel used as hydraulic fluid during Saturn V checkout?
Can I submit a paper computer science conference using an alias if using my real name can cause legal trouble in my original country
Gofer work in exchange for Letter of Recommendation
Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)
Do I need to start off my book by describing the character's "normal world"?
Ending a line of dialogue with "?!": Allowed or obnoxious?
Is it alright to say good afternoon Sirs and Madams in a panel interview?
How do the Durable and Dwarven Fortitude feats interact?
Why is the battery jumpered to a resistor in this schematic?
Regression when x and y each have uncertainties
How does the illumination of the sky from the sun compare to that of the moon?
Polar contour plot in Mathematica?
What's a good pattern to calculate a variable only when it is used the first time?
Representing an indicator function: binary variables and "indicator constraints"
Subgroup generated by a subgroup and a conjugate of it
Trying to understand how Digital Certificates and CA are indeed secure
How to use service to get the information of which app is opened?
How to `getTopActivity` name or get currently running application package name in lollipop?How to check if a service is running on Android?Is quitting an application frowned upon?How can I open a URL in Android's web browser from my application?Start an Activity with a parameterHow do I get the APK of an installed app without root access?Can't start Eclipse - Java was started but returned exit code=13What are the Android SDK build-tools, platform-tools and tools? And which version should be used?Mipmap drawables for iconsHow to make app lock app in android?Outdated Kotlin Runtime warning in Android Studio
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am a newbie to android. I am developing an app locker that uses face id instead of normal pin/pattern using kotlin.
I have got the list of installed apps in the system. But, how to get the information about which app is opened using service Please help.
android kotlin applocker
add a comment |
I am a newbie to android. I am developing an app locker that uses face id instead of normal pin/pattern using kotlin.
I have got the list of installed apps in the system. But, how to get the information about which app is opened using service Please help.
android kotlin applocker
add a comment |
I am a newbie to android. I am developing an app locker that uses face id instead of normal pin/pattern using kotlin.
I have got the list of installed apps in the system. But, how to get the information about which app is opened using service Please help.
android kotlin applocker
I am a newbie to android. I am developing an app locker that uses face id instead of normal pin/pattern using kotlin.
I have got the list of installed apps in the system. But, how to get the information about which app is opened using service Please help.
android kotlin applocker
android kotlin applocker
asked Mar 27 at 13:34
Faizan AliFaizan Ali
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Like this:
Java version
private String retriveAppInForeground()
String currentApp = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = null;
if (usm != null)
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && !appList.isEmpty())
SortedMap<Long, UsageStats> sortedMap = new TreeMap<>();
for (UsageStats usageStats : appList)
sortedMap.put(usageStats.getLastTimeUsed(), usageStats);
if (!sortedMap.isEmpty())
currentApp = sortedMap.get(sortedMap.lastKey()).getPackageName();
else
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null)
currentApp =(am.getRunningTasks(1).get(0)).topActivity.getPackageName();
Log.e("ActivityTAG", "Application in foreground: " + currentApp);
return currentApp;
Kotlin version
private fun retriveAppInForeground(): String?
var currentApp: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
val usm = this.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val appList: List<UsageStats>?
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time)
if (appList != null && appList.isNotEmpty())
val sortedMap = TreeMap<Long, UsageStats>()
for (usageStats in appList)
sortedMap.put(usageStats.lastTimeUsed, usageStats)
currentApp = sortedMap.takeIf it.isNotEmpty() ?.lastEntry()?.value?.packageName
else
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@Suppress("DEPRECATION") //The deprecated method is used for devices running an API lower than LOLLIPOP
currentApp = am.getRunningTasks(1)[0].topActivity.packageName
Log.e("ActivityTAG", "Application in foreground: " + currentApp)
return currentApp
Make sure your app has the proper permissions for accessing the usage stats:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
And that the user grants the proper permission, you can take the user to the proper settings screen to enable the permission (when setting up your app):
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
And one more thing, always make sure you search for any similar question before posting a new one.
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378514%2fhow-to-use-service-to-get-the-information-of-which-app-is-opened%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Like this:
Java version
private String retriveAppInForeground()
String currentApp = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = null;
if (usm != null)
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && !appList.isEmpty())
SortedMap<Long, UsageStats> sortedMap = new TreeMap<>();
for (UsageStats usageStats : appList)
sortedMap.put(usageStats.getLastTimeUsed(), usageStats);
if (!sortedMap.isEmpty())
currentApp = sortedMap.get(sortedMap.lastKey()).getPackageName();
else
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null)
currentApp =(am.getRunningTasks(1).get(0)).topActivity.getPackageName();
Log.e("ActivityTAG", "Application in foreground: " + currentApp);
return currentApp;
Kotlin version
private fun retriveAppInForeground(): String?
var currentApp: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
val usm = this.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val appList: List<UsageStats>?
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time)
if (appList != null && appList.isNotEmpty())
val sortedMap = TreeMap<Long, UsageStats>()
for (usageStats in appList)
sortedMap.put(usageStats.lastTimeUsed, usageStats)
currentApp = sortedMap.takeIf it.isNotEmpty() ?.lastEntry()?.value?.packageName
else
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@Suppress("DEPRECATION") //The deprecated method is used for devices running an API lower than LOLLIPOP
currentApp = am.getRunningTasks(1)[0].topActivity.packageName
Log.e("ActivityTAG", "Application in foreground: " + currentApp)
return currentApp
Make sure your app has the proper permissions for accessing the usage stats:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
And that the user grants the proper permission, you can take the user to the proper settings screen to enable the permission (when setting up your app):
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
And one more thing, always make sure you search for any similar question before posting a new one.
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
add a comment |
Like this:
Java version
private String retriveAppInForeground()
String currentApp = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = null;
if (usm != null)
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && !appList.isEmpty())
SortedMap<Long, UsageStats> sortedMap = new TreeMap<>();
for (UsageStats usageStats : appList)
sortedMap.put(usageStats.getLastTimeUsed(), usageStats);
if (!sortedMap.isEmpty())
currentApp = sortedMap.get(sortedMap.lastKey()).getPackageName();
else
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null)
currentApp =(am.getRunningTasks(1).get(0)).topActivity.getPackageName();
Log.e("ActivityTAG", "Application in foreground: " + currentApp);
return currentApp;
Kotlin version
private fun retriveAppInForeground(): String?
var currentApp: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
val usm = this.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val appList: List<UsageStats>?
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time)
if (appList != null && appList.isNotEmpty())
val sortedMap = TreeMap<Long, UsageStats>()
for (usageStats in appList)
sortedMap.put(usageStats.lastTimeUsed, usageStats)
currentApp = sortedMap.takeIf it.isNotEmpty() ?.lastEntry()?.value?.packageName
else
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@Suppress("DEPRECATION") //The deprecated method is used for devices running an API lower than LOLLIPOP
currentApp = am.getRunningTasks(1)[0].topActivity.packageName
Log.e("ActivityTAG", "Application in foreground: " + currentApp)
return currentApp
Make sure your app has the proper permissions for accessing the usage stats:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
And that the user grants the proper permission, you can take the user to the proper settings screen to enable the permission (when setting up your app):
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
And one more thing, always make sure you search for any similar question before posting a new one.
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
add a comment |
Like this:
Java version
private String retriveAppInForeground()
String currentApp = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = null;
if (usm != null)
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && !appList.isEmpty())
SortedMap<Long, UsageStats> sortedMap = new TreeMap<>();
for (UsageStats usageStats : appList)
sortedMap.put(usageStats.getLastTimeUsed(), usageStats);
if (!sortedMap.isEmpty())
currentApp = sortedMap.get(sortedMap.lastKey()).getPackageName();
else
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null)
currentApp =(am.getRunningTasks(1).get(0)).topActivity.getPackageName();
Log.e("ActivityTAG", "Application in foreground: " + currentApp);
return currentApp;
Kotlin version
private fun retriveAppInForeground(): String?
var currentApp: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
val usm = this.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val appList: List<UsageStats>?
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time)
if (appList != null && appList.isNotEmpty())
val sortedMap = TreeMap<Long, UsageStats>()
for (usageStats in appList)
sortedMap.put(usageStats.lastTimeUsed, usageStats)
currentApp = sortedMap.takeIf it.isNotEmpty() ?.lastEntry()?.value?.packageName
else
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@Suppress("DEPRECATION") //The deprecated method is used for devices running an API lower than LOLLIPOP
currentApp = am.getRunningTasks(1)[0].topActivity.packageName
Log.e("ActivityTAG", "Application in foreground: " + currentApp)
return currentApp
Make sure your app has the proper permissions for accessing the usage stats:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
And that the user grants the proper permission, you can take the user to the proper settings screen to enable the permission (when setting up your app):
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
And one more thing, always make sure you search for any similar question before posting a new one.
Like this:
Java version
private String retriveAppInForeground()
String currentApp = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = null;
if (usm != null)
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && !appList.isEmpty())
SortedMap<Long, UsageStats> sortedMap = new TreeMap<>();
for (UsageStats usageStats : appList)
sortedMap.put(usageStats.getLastTimeUsed(), usageStats);
if (!sortedMap.isEmpty())
currentApp = sortedMap.get(sortedMap.lastKey()).getPackageName();
else
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null)
currentApp =(am.getRunningTasks(1).get(0)).topActivity.getPackageName();
Log.e("ActivityTAG", "Application in foreground: " + currentApp);
return currentApp;
Kotlin version
private fun retriveAppInForeground(): String?
var currentApp: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
val usm = this.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val appList: List<UsageStats>?
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time)
if (appList != null && appList.isNotEmpty())
val sortedMap = TreeMap<Long, UsageStats>()
for (usageStats in appList)
sortedMap.put(usageStats.lastTimeUsed, usageStats)
currentApp = sortedMap.takeIf it.isNotEmpty() ?.lastEntry()?.value?.packageName
else
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@Suppress("DEPRECATION") //The deprecated method is used for devices running an API lower than LOLLIPOP
currentApp = am.getRunningTasks(1)[0].topActivity.packageName
Log.e("ActivityTAG", "Application in foreground: " + currentApp)
return currentApp
Make sure your app has the proper permissions for accessing the usage stats:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
And that the user grants the proper permission, you can take the user to the proper settings screen to enable the permission (when setting up your app):
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
And one more thing, always make sure you search for any similar question before posting a new one.
edited Mar 29 at 0:03
answered Mar 27 at 13:51
Hugo Allexis CardonaHugo Allexis Cardona
5259 silver badges18 bronze badges
5259 silver badges18 bronze badges
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
add a comment |
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
Can you explain this piece of code, SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin.
– Faizan Ali
Mar 28 at 5:29
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example.
– Hugo Allexis Cardona
Mar 29 at 0:04
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378514%2fhow-to-use-service-to-get-the-information-of-which-app-is-opened%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown