English 中文(简体)
我如何在甲状腺中找到包裹的名称?
原标题:How can I get package name in android?
  • 时间:2011-08-17 09:53:04
  •  标签:
  • java
  • android

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 android app. So how can I do this? Thank you!

最佳回答

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

你们也可以发挥职能:

public String getPackageName(Context context) {
    return context.getPackageName();
}
问题回答

以下不变之处无需考虑。

BuildConfig.APPLICATION_ID

您可以获得安装器具的包装名称:

ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
        PackageManager pm = getApplicationContext().getPackageManager();
        List<PackageInfo> packs = pm.getInstalledPackages(0);

并且如果你想找到你的包裹:

context.getPackageName();

getApplicationContext().getPackageName()将给出包装名称。

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


                      }

你们可以这样创建Util阶层:

public class AppInfo{
  private static Context cxt;
  public static String PACKAGE_INFO;
  public static String init(Context context){
    cxt = context;
    PACKAGE_INFO = cxt.getPackageName();
  }
}

那么,你可以这样使用。

public class MyActivity extends Activity{
  @Override
  public void onCreate(Bundle bundle){
    ...
    AppInfo.init(getApplicationContext());
    ...
  }
}

那么你可以这样说:

public class someClass{
  public void someMethod(){
    String packageInfo = AppInfo.PACKAGE_INFO;
    //Do what you wish
  }
}

我看到了以下两种方式,即从背景变量开始采用一揽子名称:

context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName

以及

context.getPackageName()

他们总是给予同样的答案吗? 在某些情况下最好使用另一种方法吗?

这里,在您的功用课上加上这一点。 它确保处理不同的“flavors”和“debug/release”建筑,因为其他答案并不:

@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;
    }
}

我想getPackageName()可在活动范围内直接查阅,而无需打电话getApplicationContext/code>。 自我检查。 工作良好。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...