[英]how to retrieve all installed android applications
我之前是否曾在Stackoverflow和Google上進行過搜索,但找不到任何答案,因為使用它們時出現錯誤,或者缺少某些代碼/部分,或者可能需要安裝其他庫文件。
因此,基本上我想做的是檢索android手機中所有已安裝的應用程序並將其顯示出來,但是我不知道如何去做。 我通常會遇到有關“ context”變量的錯誤,或者我的xml文件以某種方式與.java文件不匹配。
為了實現檢索所有android應用程序的目的,有人願意為我提供所有必需文件的源代碼嗎(例如xml,java,清單,可繪制,布局等)? 幫助將不勝感激
public class AppList extends Activity {
private ListView lView;
private ArrayList results = new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.list1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> 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));
}
}
首先創建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" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
此list
將用於填充已安裝的應用程序,
現在創建Activity類以獲取已安裝的應用程序,
public class AppList extends Activity {
private ListView lView;
private ArrayList results;
List<ResolveInfo> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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));
}
}
編輯-如果要從列表中打開應用程序,請單擊它。 您必須重寫ListView的onItemClick方法,並且在該方法中,您必須傳遞打開所選應用程序的意圖。
看這里
這些很可能會解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.