This question already has an answer here:
-
Package name of current running application package name Android 6.0 [duplicate]
1 answer
-
How to get package name from anywhere?
11 answers
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);
}
});
Get the package name:
getApplicationContext().getPackageName();
Refer to this as well: Get package name.
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();