如何检索已安装的带有图标的应用程序

时间:2013-01-15 11:31:41

标签: android android-intent

下面给出了我用来从手机中检索应用列表的java和xml文件中的代码。但是,它不会与它一起检索应用程序的图标。任何人都可以根据给定的代码提供解决方案,以便我如何实现检索应用程序图标的目标?非常感谢帮助

xml文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


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

    </RelativeLayout>

java文件:

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

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

 PackageManager manager = getPackageManager(); 
 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
 final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
 for ( int i=0;i<apps.size() ; i++){
    ResolveInfo info = apps.get(i);
    CharSequence lActTitle = info.loadLabel(manager);
    Drwable d = info.activityInfo.loadIcon(manager);
    }

答案 1 :(得分:0)

如果您要获取应用程序的图标,则应使用以下代码

    public class MainActivity extends Activity {
        private ListView lView;
        private ArrayList results;
        List<ResolveInfo> list;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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());
            // Add the Following line in your code.
            Drawable icon = rInfo.activityInfo.applicationInfo.loadIcon(pm);
        }
        lView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, results));
}

修改 -

看看我的一个项目代码,

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

<强> FetchApplicationsActivity.java

import java.util.ArrayList;
import java.util.List;

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

        }
    }
}