簡體   English   中英

如何檢索帶有圖標的已安裝應用程序

[英]how to retrieve installed apps with icon in them

下面給出的是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));
}

嘗試使用此代碼:

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

如果您希望獲取應用程序的圖標,則應使用以下代碼,

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

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM