问题
I am developing an android custom keyboard and i want to send stickies to my friends for example on hangouts from my keyboard. I added some png for this.
When I use the android's share api I have to select first an app to share with. Is it possible to detect the current opened application from my keyboard? For example if I am chatting with hangouts then I want to call the intent telling it to share with "com.something.hangouts"
If I am in FB messenger then telling it "com.something.facebook.messenger" etc...
I tried to get it like 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 is not working well... It gives back the launcher or the keyboard. (randomly)
I tried to get the first app from Recent Apps list but it wasn't working
回答1:
It's very easy task.
String packageName = getCurrentInputEditorInfo().packageName;
method of your InputMethodService class.
回答2:
For Android L and above, use :
ActivityManager activityManager = (ActivityManager) newContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runAppProcessesList = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo runAppProcess : runAppProcessesList) {
if (runAppProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Log.d("current foreground App", runAppProcess.processName);
}
}
回答3:
To get the current running application use the following function for android version till kitkat
public String getRunningAppPackageName()
{
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
RunningTaskInfo p = (RunningTaskInfo) tasks.get(0);
return p.baseActivity.getPackageName();}
To get the current running application use the following function for android version above kitkat
public String getRunningAppPackageName()
{
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningAppProcesses();
return activityManager.getRunnningAppProcesses().get(0).processName;}
回答4:
You can also use Accessibility Services but for that you need to redirect your users to the Accessibility Settings to turn it on. Sample code is below to get the package of the app which has been just opened .
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
String packageName = event.getPackageName();
if(packageName.equals("com.sample"))
//Do whatever you want
}}
You can read more about it here Accessibility Services
回答5:
Check this code
/**
* @param context
* @return
* @throws PackageManager.NameNotFoundException
*/
public String getRunningPackageName(Context context) throws PackageManager.NameNotFoundException {
ActivityManager mgr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String packagename = "";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
List<ActivityManager.AppTask> tasks = mgr.getAppTasks();
for (ActivityManager.AppTask task : tasks) {
packagename = task.getTaskInfo().baseIntent.getComponent().getPackageName();
if (packagename.equalsIgnoreCase(context.getPackageName().toString())) {
break;
}
}
} else {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> alltasks = activityManager.getRunningTasks(1);
packagename = alltasks.get(0).topActivity.getPackageName();
}
return packagename;
}
private void yourMethodName(Context context, Bundle notifyBundle) {
boolean isActivityFound = false;
String packageName = "";
try {
packageName = getRunningPackageName(context);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageName.equalsIgnoreCase(context.getPackageName().toString())) {
isActivityFound = true;
}
}
回答6:
You can use getRunningAppProcesses api from activitymanager but when your result will be more than 1MB (Basically in case when more than 30-35 apps are running) you will end up with 'TransactionTooLargeException' . This is android implementation not able to transfer the binder data more than 1 MB.
来源:https://stackoverflow.com/questions/35811641/how-to-detect-which-app-is-opened-on-android