I need to write some util class by myself and I need the packagename of android app. While I found the packageManager which only can be used in Activity and so on that has context. I only want to get packagename in my class which will be used in and开发者_如何转开发roid app. So how can I do this? Thank you!
Use : getPackageManager().getPackageInfo(getPackageName(), 0).packageName
Or just MyContext.getPackageName()
You can also create a function:
public String getPackageName(Context context) {
return context.getPackageName();
}
The following constant is available without the need for a context.
BuildConfig.APPLICATION_ID
you can get the package name of installed app:
ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
PackageManager pm = getApplicationContext().getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
and if you want to find the package name of your app:
context.getPackageName();
getApplicationContext().getPackageName()
will give the package name.
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
get a list of installed apps.
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
//packageInfo.packageName contains package name
}
you can create a Util Class this way:
public class AppInfo{
private static Context cxt;
public static String PACKAGE_INFO;
public static String init(Context context){
cxt = context;
PACKAGE_INFO = cxt.getPackageName();
}
}
then you can use it this way
public class MyActivity extends Activity{
@Override
public void onCreate(Bundle bundle){
...
AppInfo.init(getApplicationContext());
...
}
}
then you can call it this way:
public class someClass{
public void someMethod(){
String packageInfo = AppInfo.PACKAGE_INFO;
//Do what you wish
}
}
I have seen the following two ways to get the package name starting from a Context variable:
context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName
and
context.getPackageName()
Won't they always give the same answer? Is one way preferable to use over the other in certain circumstances?
Here add this to your util class. It makes sure to handle different “flavors” and debug/release builds, as other answers do not:
@Nullable
public static String getApplicationPackageName(@NonNull Context context)
{
final PackageInfo packageInfo = getPackageInfo(context);
if (packageInfo.applicationInfo == null)
{
return null;
}
// Returns the correct “java” package name that was defined in your
// AndroidManifest.xml.
return packageInfo.applicationInfo.packageName;
}
// Convenience method for getting the applications package info.
@NonNull
private static PackageInfo getPackageInfo(@NonNull final Context context)
{
final PackageManager packageManager = context.getPackageManager();
try
{
return packageManager.getPackageInfo(context.getPackageName(), 0);
}
catch (final PackageManager.NameNotFoundException ignored)
{
//noinspection ConstantConditions: packageInfo should always be available for the embedding app.
return null;
}
}
I think getPackageName()
is available directly within activity without calling getApplicationContext
. Checked it myself.Working good.
精彩评论