博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何获得Android手机的软件安装列表
阅读量:6227 次
发布时间:2019-06-21

本文共 6704 字,大约阅读时间需要 22 分钟。

Android的PackageManager类用于检索目前安装在设备上的应用软件包的信息。你可以通过调用getpackagemanager()得到PackageManager类的一个实例。对查询和操作安装包和相关的权限提供了方法,在下面这个Android的例子中,我们得到了在Android安装的应用程序列表。

PackageManager packageManager = getPackageManager();List
list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)复制代码

packageManager.getInstalledApplications()返回一个列表都是安装在设备上的应用程序包。如果我们把 flag GET_UNINSTALLED_PACKAGES设定,一个列表中的所有应用,包括那些设置为dont_delete_data(部分已安装的应用程序的数据目录)将返回。

1.创建 布局文件

activity_main.xml

你可以看到附件中的截图,我们将创建一个列表显示所有已安装的应用程序。

复制代码

snippet_list_row.xml

这个布局是由ListView适配器用于表示应用程序的细节。它显示应用程序图标、应用程序名称和应用程序包。

复制代码

2.java 类

AllAppsActivity.java

这是用于初始化和列表已安装的应用程序的主要应用程序类。从packagemanage得到应用细节列表是一个耗时的任务,我们将在AsyncTask里面做。同时,类使用自定义适配器“自定义listview applicationadapter”。

package com.javatechig.listapps;import java.util.ArrayList;import java.util.List;import android.app.AlertDialog;import android.app.ListActivity;import android.app.ProgressDialog;import android.content.ActivityNotFoundException;import android.content.DialogInterface;import android.content.Intent;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.AsyncTask;import android.os.Bundle;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.ListView;import android.widget.Toast;public class AllAppsActivity extends ListActivity {	private PackageManager packageManager = null;	private List
applist = null; private ApplicationAdapter listadaptor = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); packageManager = getPackageManager(); new LoadApplications().execute(); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { boolean result = true; switch (item.getItemId()) { case R.id.menu_about: { displayAboutDialog(); break; } default: { result = super.onOptionsItemSelected(item); break; } } return result; } private void displayAboutDialog() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.about_title)); builder.setMessage(getString(R.string.about_desc)); builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stacktips.com")); startActivity(browserIntent); dialog.cancel(); } }); builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.show(); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); ApplicationInfo app = applist.get(position); try { Intent intent = packageManager .getLaunchIntentForPackage(app.packageName); if (null != intent) { startActivity(intent); } } catch (ActivityNotFoundException e) { Toast.makeText(AllAppsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(AllAppsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } } private List
checkForLaunchIntent(List
list) { ArrayList
applist = new ArrayList
(); for (ApplicationInfo info : list) { try { if (null != packageManager.getLaunchIntentForPackage(info.packageName)) { applist.add(info); } } catch (Exception e) { e.printStackTrace(); } } return applist; } private class LoadApplications extends AsyncTask
{ private ProgressDialog progress = null; @Override protected Void doInBackground(Void... params) { applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA)); listadaptor = new ApplicationAdapter(AllAppsActivity.this, R.layout.snippet_list_row, applist); return null; } @Override protected void onCancelled() { super.onCancelled(); } @Override protected void onPostExecute(Void result) { setListAdapter(listadaptor); progress.dismiss(); super.onPostExecute(result); } @Override protected void onPreExecute() { progress = ProgressDialog.show(AllAppsActivity.this, null, "Loading application info..."); super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } }}复制代码

ApplicationAdapter.java

package com.javatechig.listapps;import java.util.List;import android.content.Context;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.TextView;public class ApplicationAdapter extends ArrayAdapter
{ private List
appsList = null; private Context context; private PackageManager packageManager; public ApplicationAdapter(Context context, int textViewResourceId, List
appsList) { super(context, textViewResourceId, appsList); this.context = context; this.appsList = appsList; packageManager = context.getPackageManager(); } @Override public int getCount() { return ((null != appsList) ? appsList.size() : 0); } @Override public ApplicationInfo getItem(int position) { return ((null != appsList) ? appsList.get(position) : null); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (null == view) { LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.snippet_list_row, null); } ApplicationInfo applicationInfo = appsList.get(position); if (null != applicationInfo) { TextView appName = (TextView) view.findViewById(R.id.app_name); TextView packageName = (TextView) view.findViewById(R.id.app_paackage); ImageView iconview = (ImageView) view.findViewById(R.id.app_icon); appName.setText(applicationInfo.loadLabel(packageManager)); packageName.setText(applicationInfo.packageName); iconview.setImageDrawable(applicationInfo.loadIcon(packageManager)); } return view; }};复制代码

转载地址:http://ysnna.baihongyu.com/

你可能感兴趣的文章
什么是Solr
查看>>
oracle 12cR1&12cR2核心高实用性新特性
查看>>
pandas Series的sort_values()方法
查看>>
SQL SERVER CHAR ( integer_expression )各版本返回值差异的案例
查看>>
pytest文档7-pytest-html生成html报告
查看>>
微信小程序弹窗组件
查看>>
安装使用ionic3
查看>>
结构体初始化
查看>>
java中this的N种使用方法
查看>>
Windows IIS安装php
查看>>
mingw 设置python 设置git环境变量
查看>>
linux 系统下如何进行用户之间的切换
查看>>
设计一个算法移除字符串中的重复字符,并写出测试用例。
查看>>
goole机器学习视频链接【学习笔记】
查看>>
查看django版本的方法
查看>>
kafka channle的应用案例
查看>>
WPF 圆角textbox
查看>>
熊彼特的创新理论:非连续性模型
查看>>
Windows10内置ubuntu子系统安装后中文环境设置
查看>>
Spring Security教程(八):用户认证流程源码详解
查看>>