android - 如何拉取已安装应用程序名称、包名称和可绘制图标的列表

标签 android android-listview android-package-managers android-applicationinfo

我正在尝试弄清楚如何将此代码实现到我现有的源代码中。目前我有一些来源显示所有已安装应用程序的 ListView ,点击将向应用程序发送 Intent 。我需要一些关于如何拉出图标并将其添加到 ListView 的支持。

任何帮助、源代码编辑、链接等都可以帮助我解决这个问题。

谢谢

ListInstalledActivitiesActivity

public class ListInstalledActivitiesActivity extends ListActivity {

    // Buffer used to store package and class information, and also determine the number of installed activities
    private ArrayList<String[]> _activitiesBuffer = null;

    // Buffers for package and class information
    private String[] _packages = null;
    private String[] _classes = null;

    // Index used to fill buffers
    private int _index = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);
        // Get all installed activities (package and class information for every activity)
        getAllInstalledActivities();              

        // Set content to GUI
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, _classes));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        // Add listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // When clicked, show a toast with the selected activity
                Toast.makeText(
                    getApplicationContext(), 
                    ((TextView) view).getText(), 
                    Toast.LENGTH_SHORT).show();

                // When clicked, start selected activity, if allowed or possible
                try {

                    Intent intent = new Intent().setClassName(
                            _packages[position], // package 
                            _classes[position]); // class
                    startActivity(intent);

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Unable to start selected application.", Toast.LENGTH_SHORT);
                }

          } // public void onItemClick(AdapterView<?> parent, View view, int position, long id)

        });

    } // public void onCreate(Bundle savedInstanceState)

    /*
     * Get all installed activities
     */
    private void getAllInstalledActivities() {


        // Initialize activities buffer
        _activitiesBuffer = new ArrayList<String[]>();

        final Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities( intent, 0);

        Iterator<ResolveInfo> iterator1 = pkgAppsList.iterator();
        while (iterator1.hasNext()) {

            ResolveInfo resolveInfo = iterator1.next();

            String[] buf = new String[] {
                    resolveInfo.activityInfo.packageName, 
                    resolveInfo.activityInfo.name};

            _activitiesBuffer.add(buf);

        } // while (iterator1.hasNext())

        _packages = new String[_activitiesBuffer.size()];
        _classes = new String[_activitiesBuffer.size()];

        Iterator<String[]> iterator2 = _activitiesBuffer.iterator();
        while (iterator2.hasNext()) {

            String[] buf = iterator2.next();

            // Store package information
            _packages[_index] = buf[0]; 

            // Store class information
            _classes[_index] = buf[1];

            _index++;

        } // while (iterator2.hasNext())

    } // private void getAllInstalledActivities()

      }

ma​​in3.xml

 <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/android:list"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" />

      <!-- <ImageView -->
      <!--android:id="@+id/ImageView02" -->
      <!--android:layout_width="fill_parent" -->
      <!--android:layout_height="wrap_content" -->
      <!--android:layout_marginBottom="10dp" -->
      <!--android:paddingBottom="5dp" -->


      </LinearLayout>

最佳答案

要获取已安装应用程序的名称和图标,您需要使用 Package Manager 类,这里是一段代码,可以让您获取应用程序的名称和图标

   import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FetchApplicationsActivity extends Activity {

    TextView data;
    ImageView image1;
    LinearLayout holdlayout;
    View l1;
    private ArrayList results;
    List<ResolveInfo> list;
    TextView result;
    String str = "";
    Drawable icon;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        l1 = findViewById(R.id.Layout1);
        results = new ArrayList();
        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) {
            str = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()
                    + "\n";
            results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm)
                    .toString());
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                    .loadLabel(pm).toString());
            icon = rInfo.activityInfo.applicationInfo.loadIcon(pm);
            holdlayout = new LinearLayout(getApplicationContext());
            holdlayout.setOrientation(LinearLayout.HORIZONTAL);
            data = new TextView(getApplicationContext());
            data.setText(str);
            image1 = new ImageView(getApplicationContext());
            image1.setBackgroundDrawable(icon);
            ((ViewGroup) holdlayout).addView(image1);
            ((ViewGroup) holdlayout).addView(data);
            ((ViewGroup) l1).addView(holdlayout);

        }
    }
}

Edit- 您可以将 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" >



    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <LinearLayout
        android:id="@+id/Layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >



    </LinearLayout>
    </ScrollView>

</LinearLayout>

我在这里创建了动态 textviewsImageviewslayouts 来显示名称和图标。您可以创建自己的自定义列表来显示这一点。

Edit2- 这是how to create customized list 上的一个很好的链接也看看here .我认为这些将解决问题。

关于android - 如何拉取已安装应用程序名称、包名称和可绘制图标的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065028/

相关文章:

Android ListView OnItemClick 更改 View 属性

android - 如何启用 Android 下载管理器

android - 重新创建 Activity 时出现生命周期异常

Android TextView,autoLink ="all"将所有数字显示为可点击

android - Android O 启动时 Onesignal 崩溃

java - 按钮在 ListView 中不起作用

java - 使用jsoup从webview解析登录的网站

android - Facebook 的 Android 应用程序是否使用 ListView 或是否有其他方法在时间轴中创建漂亮的帖子列表?

android - PackageManager 死亡 - DeadObjectException - 小包裹交易失败

android - 下载的 SDK 源代码中缺少接口(interface)文件