2

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

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”。

编辑 我的设备中有很多帐户,例如 facebook、viber、whats app、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)

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

4

0 回答 0