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

2024-03-06

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

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

谢谢

列出已安装的活动活动

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()

      }

main3.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 as,

?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>

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

Edit2-这是一个很好的链接如何创建自定义列表 http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/并且还看here https://stackoverflow.com/questions/4407865/how-to-customize-listview-row-android。我认为这些可以解决问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 如何等待流完成管道传输? (节点)

    我有一个 Promise 的 for 循环数组 所以我使用 Promise all 来遍历它们 然后调用 then let promises promises push promise1 promises push promise2 pro
  • 我可以为背景大小实现纯 CSS 后备吗?

    这对于支持的浏览器来说效果很好background size 否则 图像会缩放 2 倍 a background image url img2x jpg 1000x1000 background size 100 height 500px
  • Android自定义EditText和后退按钮覆盖

    我想在显示软键盘时覆盖后退按钮 基本上 当按下后退按钮时 我希望键盘关闭 并且我想将一些文本附加到用户在编辑文本字段中键入的内容上 所以基本上我需要知道键盘何时关闭 经过搜索后 我意识到没有可用的 API 唯一真正的方法是创建 EditTe
  • 如何定位按钮精灵 css

    button1 background E68A00 url wooden jpg repeat x border 2px solid eee height 28px width 115px margin 50px 0 0 50px padd
  • Shell 脚本:获取 python: 命令未找到错误

    当我从 shell 脚本调用 Python 脚本时 它运行良好 python script py 但是当我从 Gerrit 中提取相同的脚本 然后添加调用 Python 脚本的代码后 它给了我以下错误 script sh line 126
  • php require_once 尝试仅在我的生产服务器上包含第二次

    我在各种包含文件的顶部都有这段代码 require once functions php 有时我需要包含几个包含文件来生成页面 并且在我的本地服务器上这工作正常 因为上面的代码告诉它只包含一次functions php 因此它不会尝试声明函
  • MS Entity Framework VS NHibernate 及其派生贡献(FluentNHibernate、Linq for NHibernate)

    我刚刚读过这个article http visualstudiomagazine com Articles 2009 12 01 Entity Sequel aspx Page 1关于实体框架 4 实际上是版本 2 实体框架 http ms
  • 将 for-each 循环替换为 lambda 表达式

    我只是重构一些旧项目以使用 Java 8 的功能 int counter 1 for Checker checker checkers if counter lt checkers size checker setNextChecker c
  • 不要使用 Xcode 8 复制 swift 库吗?

    How to not在 Xcode 8 中自动嵌入 Swift 动态库 我尝试过设置ALWAYS EMBED SWIFT STANDARD LIBRARIES为 否 无论如何默认为 否 但它仍然将 Swift 动态库复制到应用程序包中 我正
  • Require.js 延迟加载远程 url

    我的本地文件系统上有一个名为 moment js 的文件 并使用 require js 加载它 如下所示 initialize function require moment function data console log data 但
  • Java中杀死进程的正确方法

    在 Java 中终止进程的最佳方法是什么 获取 PID 然后用以下命令杀死它Runtime exec Use destroyForcibly 这两种方法有什么区别 还有其他解决方案吗 如果您要终止的进程已由您的应用程序启动 那么你可能已经参
  • Tapestry5 无法将视频流传输到 iPad

    我想通过后端带有 Tapestry5 5 3 5 的 HTML5 视频标签将视频流式传输到我的 iPad 通常 服务器端框架甚至不应该在其中发挥作用 但不知何故它确实发挥了作用 无论如何 希望这里有人能帮助我 请记住 我的项目很大程度上是一
  • Kafka 连接:提供了配置 XXX,但不是 AdminClientConfig 中的已知配置

    启动 Kafka Connect 时 我看到很多警告 10 33 56 706 DistributedHerder WARN org apache kafka clients admin AdminClientConfig The conf
  • 如何使用 AngularJS 创建可排序的手风琴?

    I found 用户界面可排序 https github com angular ui ui sortable并使其能够很好地处理简单的列表等 我的应用程序已经使用了ui引导程序 http angular ui github io boot
  • 使用 AirPrint 打印 PDF 会导致输出较小

    我尝试打印 pdfUIPrintInteractionController它加载在UIWevView 好消息是我可以打印 坏消息是打印输出太小 any help would be appreciated IBACTION printPDF
  • 如何将对象传递给 numpy 点函数

    假设我已经定义了我的对象 import numpy as np class myTensor def init self data self data np array data self parent 如何将 myTensor 作为输入传
  • Sphinx Pygments 词法分析器过滤器扩展?

    我有一种类似 Lisp 的语言 我想在 Sphinx 代码片段文档中强调使用 Pygments 我的方法是扩展现有的 CommonLispLexer 以使用 NameHighlightFilter 添加内置名称 但是 它不起作用 所以我一定
  • cordova - 失败:不支持 q

    当我构建 cordova 时 此消息视图 不支持使用 requireCordovaModule 加载非cordova模块 q 相反 将此模块添加到您的依赖项中并使用常规 require 来加载它 如何解决这个问题 附言 我在这个构建问题之前
  • Android SDK 管理器无法下载新文件

    我试图获取最新的 android 源代码 5 0 只是为了看看它看起来如何 但是当我尝试从 Android SDK 下载源代码时 它给了我一个错误 说local找不到网址 这是日志 Fetching https dl ssl google
  • 如何拉取已安装应用程序名称、包名称和可绘制图标的列表

    我试图弄清楚如何将此代码实现到我现有的源代码中 目前 我有一些源显示所有已安装应用程序的列表视图 单击将向应用程序发送意图 我需要一些关于如何拉出图标并将其添加到列表视图中的支持 任何帮助 源代码编辑 链接等都可以帮助我解决这个问题 谢谢