[英]How to get application name in android?
當應用程序安裝在設備中並成功后。 我必須獲取該應用程序包名稱,並從包名稱中識別“應用程序名稱”。 我怎樣才能做到這一點
代碼:-
private String TAG = CAppReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.intent.action.PACKAGE_ADDED")){
Log.d(TAG,"App package::" + intent.getData().toString());
}
}
String pakageName =getPackageName();
String[] separated = pakageName.split(".");
separated[0];
separated[1];
聽到獲取應用程序名稱的代碼,首先獲取包名稱,然后將其拆分以獲取應用程序名稱。
在您的清單中,添加具有相關意圖服務的接收器(在應用程序標簽內):
<receiver android:name=".apps.AppListener">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
根據谷歌文檔:
PACKAGE_INSTALL 廣播動作:設備上安裝了新的應用程序包。 數據包含包的名稱。 請注意,新安裝的軟件包不會收到此廣播。
然后使用您的 onReceive 代碼方法。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AppListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.v(TAG, "there is a broadcast");
}
}
使用以下方法獲取應用程序包名稱。
intent.getData().getSchemeSpecificPart()
並使用以下代碼獲取其他應用程序特定信息。
try {
ApplicationInfo app = this.getPackageManager().getApplicationInfo("com.example.name", 0);
Drawable icon = packageManager.getApplicationIcon(app);
String name = packageManager.getApplicationLabel(app);
return icon;
} catch (NameNotFoundException e) {
Toast toast = Toast.makeText(this, "error in getting icon", Toast.LENGTH_SHORT);
toast.show();
e.printStackTrace();
}
如果您有包名稱,則可以獲得您的應用程序名稱或應用程序標簽。 這是如何
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo("YourPackageName", PackageManager.GET_META_DATA));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.