Android:从帐户类型获取应用名称

时间:2015-11-03 05:42:55

标签: android package android-package-managers android-account

我的应用需要知道帐户类型的应用名称。我从代码中获取帐户类型:

AccountManager am = AccountManager.get(this);
Account[] acc = am.getAccounts();
if (acc.length > 0) {
    for (int accLength = 0; accLength < acc.length; accLength++) {
            Log.d("Accounts : ", "" + acc[accLength].name+"-"+ acc[accLength].type);                
    }
}

例如,我将帐户类型设为“com.whatsapp”,但我希望名称为“Whats App”。

修改 我的设备中有很多帐户,例如脸书,viber,什么应用程序,Skype等。

对于Skype,我的帐户类型为com.skype.contacts.sync

并将代码用作:

PackageManager packageManager = getApplicationContext().getPackageManager();
try {
    String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(acc[accLength].type, PackageManager.GET_META_DATA));
    Log.d("AppName", "" + appName);
} catch (NameNotFoundException e) {
    e.printStackTrace();
}

获取应用名称,我收到了以下错误:

11-03 11:38:10.681: W/System.err(4684): android.content.pm.PackageManager$NameNotFoundException: com.skype.contacts.sync
11-03 11:38:10.681: W/System.err(4684):     at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:243)
11-03 11:38:10.681: W/System.err(4684):     at com.example.cd.SplashScreenActivity.initializeScreen(SplashScreenActivity.java:48)
11-03 11:38:10.681: W/System.err(4684):     at com.example.cd.SplashScreenActivity.onCreate(SplashScreenActivity.java:33)
11-03 11:38:10.681: W/System.err(4684):     at android.app.Activity.performCreate(Activity.java:5541)
11-03 11:38:10.681: W/System.err(4684):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-03 11:38:10.681: W/System.err(4684):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
11-03 11:38:10.681: W/System.err(4684):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)
11-03 11:38:10.681: W/System.err(4684):     at android.app.ActivityThread.access$900(ActivityThread.java:172)
11-03 11:38:10.681: W/System.err(4684):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
11-03 11:38:10.681: W/System.err(4684):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-03 11:38:10.681: W/System.err(4684):     at android.os.Looper.loop(Looper.java:146)
11-03 11:38:10.686: W/System.err(4684):     at android.app.ActivityThread.main(ActivityThread.java:5692)
11-03 11:38:10.686: W/System.err(4684):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 11:38:10.686: W/System.err(4684):     at java.lang.reflect.Method.invoke(Method.java:515)
11-03 11:38:10.686: W/System.err(4684):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
11-03 11:38:10.686: W/System.err(4684):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
11-03 11:38:10.686: W/System.err(4684):     at dalvik.system.NativeStart.main(Native Method)

我在Google上搜索但无法完成任务。请建议我该怎么办...

0 个答案:

没有答案