简体   繁体   English

如何检索所有已安装的android应用程序

[英]how to retrieve all installed android applications

Did my search on the Stackoverflow and Google before already but I could not find any answer that worked out for me as I get errors when using them or that there are certain codes/parts missing or, that there could be a need to install additional library files. 我之前是否曾在Stackoverflow和Google上进行过搜索,但找不到任何答案,因为使用它们时出现错误,或者缺少某些代码/部分,或者可能需要安装其他库文件。

So basically what I want to do is to retrieve all the installed apps in the android phone and display it out but I have no clue as to how to go about doing it. 因此,基本上我想做的是检索android手机中所有已安装的应用程序并将其显示出来,但是我不知道如何去做。 I usually run into error regarding the "context" variable or that my xml files does not match the .java files in one way or another. 我通常会遇到有关“ context”变量的错误,或者我的xml文件以某种方式与.java文件不匹配。

Would anyone be kind enough to give me the source codes for all the required files(eg xml, java, manifest, drawable, layout etc) in order to achieve the objective of retrieving all android apps? 为了实现检索所有android应用程序的目的,有人愿意为我提供所有必需文件的源代码吗(例如xml,java,清单,可绘制,布局等)? Help would be very much appreciated 帮助将不胜感激

public class AppList extends Activity {
 private ListView lView;
 private ArrayList results = new ArrayList();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  lView = (ListView) findViewById(R.id.list1);
  PackageManager pm = this.getPackageManager();

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

  List<ResolveInfo>  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));
    }
}

First create main.xml file as follows, 首先创建main.xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

            <ListView
                android:id="@+id/list1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>

</LinearLayout>

This list will be used to populate the installed applications, list将用于填充已安装的应用程序,

Now create the Activity class to fetch the installed applications, 现在创建Activity类以获取已安装的应用程序,

public class AppList extends Activity {
    private ListView lView;
    private ArrayList results;
    List<ResolveInfo> list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            results = new ArrayList();
        lView = (ListView) findViewById(R.id.list1);
        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));
    }
}

Edit- If you want to open the application from the list, once you click it. 编辑-如果要从列表中打开应用程序,请单击它。 You have to override the onItemClick method of the ListView and in that method, you have to pass the intent to open the selected application. 您必须重写ListView的onItemClick方法,并且在该方法中,您必须传递打开所选应用程序的意图。

Have a look here, 看这里

These will most probably solve your issue. 这些很可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM