2013-01-15 108 views
0

下面給出的是我用來從手機中檢索應用程序列表的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)); 
} 
+0

你的意思是rInfo.getIconResource()返回0? – ben75

回答

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

     } 
    } 
} 
+0

以及變量rInfo以紅色下劃線。爲了解決這個問題,有什麼具體的事情需要完成嗎? – user1977094

+0

我編輯了我的答案。現在檢查 –

+0

結果不會改變。我是否必須編輯xml文件中的任何內容或者是否需要完成Drawable的某些操作? – user1977094

相關問題