如何创建与每个已安装的 android 应用程序(在用户手机上)关联的活动(android 类)的列表(文件名、位置)?
Posted
技术标签:
【中文标题】如何创建与每个已安装的 android 应用程序(在用户手机上)关联的活动(android 类)的列表(文件名、位置)?【英文标题】:How can I create a list (file name, location) of activities (android classes) associated with each installed android app (on a user's phone)? 【发布时间】:2015-08-02 03:13:50 【问题描述】:到目前为止,我已经编写了一个应用程序,它会获取用户手机上所有已安装的 android 应用程序(名称和图标)的列表;
但我也想创建一个与每个已安装的 android 应用程序(文件名,位置地址)。
我怎样才能做到这一点?
到目前为止的代码:
MainActivity.java
public class MainActivity extends ListActivity
private PackageManager packageManager = null;
private List<ApplicationInfo> 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)
return false;
@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(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
catch (Exception e)
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list)
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
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<Void, Void, Void>
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params)
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(MainActivity.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(MainActivity.this, null,
"Loading application info...");
super.onPreExecute();
@Override
protected void onProgressUpdate(Void... values)
super.onProgressUpdate(values);
ApplicationAdapter.java
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo>
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
public ApplicationAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> 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 data = appsList.get(position);
if (null != data)
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(data.loadLabel(packageManager));
packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(packageManager));
return view;
;
【问题讨论】:
【参考方案1】:我假设只有第三方应用程序的活动将其 android:exported 属性设置为 true,您才能实现您的计划。默认情况下,所有活动都将 android:exported 设置为 false
【讨论】:
以上是关于如何创建与每个已安装的 android 应用程序(在用户手机上)关联的活动(android 类)的列表(文件名、位置)?的主要内容,如果未能解决你的问题,请参考以下文章