1

How to get current activity package name? as getRunningAppProcesses() not working in Android 6.0.

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) {

        //get package name of current running application.
        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        packageName = mActivityManager.getRunningAppProcesses().get(1).processName;

        Log.e("Package name:===========      ", "Package name     " + packageName);

    }
});
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Mohd Sakib Syed
  • 1,679
  • 1
  • 25
  • 42

2 Answers2

4

Get the package name:

getApplicationContext().getPackageName();

Refer to this as well: Get package name.

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
2

This code is unnecessary:

//get package name of current running application.
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
packageName = mActivityManager.getRunningAppProcesses().get(1).processName;

Try this:

grdPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //get package name of current running application.
        Log.e("Package name:===========      ", "Package name     " + getApplicationContext().getPackageName());
           }
});

If that doesn't work, you know there's a problem with your ontemclicklistener, so try this in your oncreate method or as a log to show proof that getApplicationContext().getPackageName() will get the correct application package name.

Toast.makeText(this, getApplicationContext().getPackageName(),Toast.LENGTH_SHORT).show();