How to Get Package Name from Anywhere

How to get package name from anywhere?

An idea is to have a static variable in your main activity, instantiated to be the package name. Then just reference that variable.

You will have to initialize it in the main activity's onCreate() method:

Global to the class:

public static String PACKAGE_NAME;

Then..

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PACKAGE_NAME = getApplicationContext().getPackageName();
}

You can then access it via Main.PACKAGE_NAME.

How to get Package name from activity name

The only reliable way was Vladyslav Matviienko suggestion, which is storing package name along with each activity name in a hashmap to be like .. Thank you all for your help.

android, how to get package name?

The simple, or another way is to pass Context into the helper class constructor:

MyClassConstructor(Context context){

String packageName = context.getPackageName();
}

Get package name and version of Parent Application from it's dependency

Given a Context, call getPackageName() on the Context to find out the package name of the app. Given the Context and the package name, you can call getPackageManager().getPackageInfo() on the Context to get a PackageInfo object for the app, which contains versionCode and versionName.

How can I get package name in android?

Use : getPackageManager().getPackageInfo(getPackageName(), 0).packageName
Or just MyContext.getPackageName()

You can also create a function:

public String getPackageName(Context context) {
return context.getPackageName();
}

get package installer

I found a method for that:
PackageManager.getInstallerPackageName(String packageName);
This returns the package name of the installer application.



Related Topics



Leave a reply



Submit