This question already has an answer here:
- Android 6.0: Get package name of current activity [duplicate] 2 answers
I am using custom keyboard to share image to any app like Facebook,whatsApp and i want to know the current open application package name on onIteamClickListener of my custom keyboard to share data without chooser dialog. Below is my code
grdPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ActivityManager am = (ActivityManager)MainActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
String packageName = tasks.get(1).processName;
}
});