Android-アプリケーション名を取得する方法は?(パッケージ名ではありません)


120

私のマニフェストで私は持っています:

  <application
    android:name=".MyApp"
    android:icon="@drawable/ic_launcher_icon"
    android:label="@string/app_name"
    android:debuggable="true">

ラベル要素を取得するにはどうすればよいですか?

注:私のコードは他の誰かの内部で実行されているため、@ string / app_nameにアクセスできません

回答:


182

リソースに明示的に名前を付けたり、パッケージ名の例外を心配したりする必要がない他の回答よりも簡単な方法があります。リソースの代わりに文字列を直接使用した場合にも機能します。

ただやる:

public static String getApplicationName(Context context) {
    ApplicationInfo applicationInfo = context.getApplicationInfo();
    int stringId = applicationInfo.labelRes;
    return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
}

お役に立てれば。

編集する

Snicolasからのコメントに照らして、IDが0の場合に解決しようとしないように上記を変更しましたnonLocalizedLabel。代わりに、バックオフとして使用します。try / catchでラップする必要はありません。


3
でラベルを実際に使用している限り、これは問題なく機能しますandroid:name。文字列をハードコーディングした場合、失敗します。
Snicolas 2013

2
Trueですが、通常、アプリ名は通常、文字列リソースを介して指定されます。固定された文字列はlintによってフラグが立てられ、推奨されません。
darrenp

1
文字列が見つからない場合、android.content.res.Resources $ NotFoundExceptionがスローされることに注意してください
zenocon

zenoconのアドバイスに従い、内に配置することをお勧めしtry ... catchます。
superarts.org 2016

nonLocalizableLabelのドキュメントについて何か知っていますか?:「おそらくgetApplicationLabel(ApplicationInfo)が必要です」
David

55

android:label = "MyApp"のような理由でAndroidManifest.xmlのstrings.xml / hardcodedで言及されていない場合

public String getAppLable(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = packageManager.getApplicationInfo(context.getApplicationInfo().packageName, 0);
    } catch (final NameNotFoundException e) {
    }
    return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}

または、文字列リソースIDがわかっている場合は、直接取得できます。

getString(R.string.appNameID);

string.xmlからアプリ名を読み取っているときに、ジャンク文字が表示されることがあります。理由はわかりません。例:सà¥&#141;à¤&#149;à¥&#136;नर
harikrishnan


25

任意のコンテキストから:

getApplicationInfo().loadLabel(getPackageManager()).toString();

15

パッケージ名がわかっている場合は、次のスニペットを使用します

ApplicationInfo ai;
try {
    ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
    ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

1
@RishabhTayal pmはPackageManagerオブジェクトを示します。
Vipul Shah 2013

@VipulShahイム不明毎回取得
ニダ

6

パッケージ名ではなくアプリケーション名のみが必要な場合は、このコードを記述します。

 String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();

6

Kotlin、アプリケーション名を取得するには、次のコードを使用します。

        // Get App Name
        var appName: String = ""
        val applicationInfo = this.getApplicationInfo()
        val stringId = applicationInfo.labelRes
        if (stringId == 0) {
            appName = applicationInfo.nonLocalizedLabel.toString()
        }
        else {
            appName = this.getString(stringId)
        }


2

RunningAppProcessInfoを使用してアプリケーション名を取得します。

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}


1

に追加されたソースコメントは、次のNonLocalizedLabelように指示します。

return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo());

0

このPackageManager#getActivityInfo()方法を試しましたか?アプリケーションの名前を含むフィールドがあります。

よく似た質問への回答はこちらをご覧ください

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.