I am developing a custom Android keyboard and I want to send sticky ideas to my friends, for example, in a video call from my keyboard. I have added some png for this.
When I use the android share for Android, I must first select the application to share. Is it possible to detect the current open application from my keyboard? For example, if I’m hanging out with video calls, then I want to name an intention telling him to share with “com.something.hangouts”
If I am in the FB envoy, I tell him "com.something.facebook.messenger", etc.
I tried to do this:
fun getOpenedApplication() { var am = this.getSystemService(ACTIVITY_SERVICE) as ActivityManager; var l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED); var i = l.iterator(); var pm = this.packageManager; while (i.hasNext()) { try { var intent = i.next().baseIntent; var list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); var c = pm.getApplicationLabel(pm.getApplicationInfo( list[0].activityInfo.packageName, PackageManager.GET_META_DATA)); Toast.makeText(this, "Application name: " + c.toString(), Toast.LENGTH_LONG).show(); } catch (e: Exception) { Toast.makeText(this, "Application name not found: " + e.toString(), Toast.LENGTH_LONG).show(); } } }
But it does not work well ... It returns the launcher or keyboard. (Randomly)
I tried to get the first application from the list of recent applications, but it did not work
source share