在AutoCompleteTextView中单击应用程序名称时显示包名称

时间:2014-05-29 13:22:54

标签: java android

我创建了一个显示所有已安装应用的AutoCompleteTextView。

我还创建了一个Toast(并将其实施到onItemClickListener),它会显示所选应用的 PackageName

如何从包管理器中检索包名?请帮忙

        final AutoCompleteTextView lView;
        final ArrayList results;
        List<ResolveInfo> list;
        final String pname = "";


        results = new ArrayList();
    lView = (AutoCompleteTextView)this.findViewById(R.id.languages);
    PackageManager pm = this.getPackageManager();




    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    list = pm.queryIntentActivities(intent,
        PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list) {
        results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm)
                .toString());
        Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
    }
    lView.setAdapter(new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, results));



    lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "PACKAGENAME", Toast.LENGTH_SHORT).show();
        }
  });

2 个答案:

答案 0 :(得分:1)

您将从ResolveInfo获得包裹信息。

你有 List list; 因此,将您的ItemClickListener实施更改为此

lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String packageName = list.get(position).activityInfo.applicationInfo.packageName;
            Toast.makeText(getApplicationContext(), packageName, Toast.LENGTH_SHORT).show();
    }
});

答案 1 :(得分:0)

使用

yourContext.getPackageName();

更新:

获取您的背景信息

public void onCreate(Bundle savedInstanceState)
{ 
        Contenxt yourContext= this;

        yourContext.getPackageName();
}