Package name of current running application packag

2019-03-06 18:00发布

问题:

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;


               }
    });

回答1:

 packageInfo=mActivity.getPackageManager().getPackageInfo(mActivity.getPack
    ageName(), PackageManager.GET_ACTIVITIES);