我正在开发一个应用程序,需要用户在手机上安装的应用程序列表。 我曾在网上看过教程,但没有一个是足够具体的,即显示代码但从未显示在哪里放
但是对编程这么新,我不能使用它,因为我不知道将它放在我的代码中的哪个位置,尽管我发现一个代码无法显示版本或图标。我真的可以用别人来抓住这个。
我需要的是一个 arraylist ,它显示用户安装的应用程序,图标和版本。
以下是我工作的代码:
package com.fina;
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.pm.PackageInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class scan extends Activity {
public ArrayList res = new ArrayList ();公共ListView列表; public String app_labels [];
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.allc);
getPackages(); list = (ListView)findViewById(R.id.listView1); try{ list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, app_labels)); }catch(Exception e){ System.out.println("Err ++> " + e.getMessage()); Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); }
} private ArrayList getPackages(){ 数组列表 apps = getInstalledApps(false); final int max = apps.size(); for(int i = 0; i&lt; max; i ++){ apps.get(ⅰ); } 返回应用; }
私有ArrayList getInstalledApps(boolean getSysPackages){
List <PackageInfo> packs = getPackageManager().getInstalledPackages(0); try{ app_labels = new String[packs.size()]; }catch(Exception e){ Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); } for(int i=0;i < packs.size();i++) { PackageInfo p = packs.get(i); if ((!getSysPackages) && (p.versionName == null)) { continue ; } PackageInfoStruct newInfo = new PackageInfoStruct(); newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); newInfo.pname = p.packageName; newInfo.versionName = p.versionName; newInfo.versionCode = p.versionCode; newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); res.add(newInfo); app_labels[i] = newInfo.appname; } return res; } } //This class is for storing the data for each application class PackageInfoStruct { String appname = ""; String pname = ""; String versionName = ""; int versionCode = 0; Drawable icon; }
这是我链接到它的allc.xml:
<AbsoluteLayout android:id="@+id/absoluteLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/allcast" android:layout_width="61dp" android:layout_height="66dp" android:layout_x="0dp" android:layout_y="0dp" android:background="@drawable/invis" /> <Button android:id="@+id/downcast" android:layout_width="63dp" android:layout_height="60dp" android:layout_x="64dp" android:layout_y="3dp" android:background="@drawable/invis" /> <Button android:id="@+id/commcast" android:layout_width="51dp" android:layout_height="57dp" android:layout_x="255dp" android:layout_y="3dp" android:background="@drawable/invis" /> <Button android:id="@+id/achcast" android:layout_width="62dp" android:layout_height="58dp" android:layout_x="192dp" android:layout_y="2dp" android:background="@drawable/invis" /> <Button android:id="@+id/recocast" android:layout_width="59dp" android:layout_height="60dp" android:layout_x="128dp" android:layout_y="3dp" android:background="@drawable/invis" /> <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_x="0dp" android:layout_y="71dp" > </ListView> </AbsoluteLayout>
请尽可能详细地说明您的答案,而不仅仅是指向我可以使用的代码的网站的链接,我真的需要有人亲自抓住这个。
答案 0 :(得分:1)
在我的教程,分步说明中,提供完整的源代码:https://xjaphx.wordpress.com/2011/06/12/create-application-launcher-as-a-list