如何列出Android中的所有普通应用程序?

时间:2014-10-03 04:09:08

标签: android android-activity android-package-managers

我使用此程序获取设备中的所有应用

    PackageManager pm;
    pm =getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(0);

    for(ApplicationInfo ai:packages)
    {
        Log.v("listpackage","name: "+ai.loadLabel(pm).toString()+", package:"+ai.packageName);
    }

然后我使用此程序打开这些应用程序

    startActivity(getPackageManager().getLaunchIntentForPackage("com.xxx.yyy"));

但是,当我列出应用程序时,我发现了许多特殊的应用程序。

e.g。

name: Tags, package:com.android.apps.tag
name: com.android.backupconfirm, package:com.android.backupconfirm
name: Bluetooth Share, package:com.android.bluetooth

如果我打开这些应用程序,我的应用程序将被终止,我也不知道这些应用程序是什么。我在手机中找不到这些应用

如何在没有这些应用程序的情况下获取所有应用程序?

或者如何检测正常App或特殊App的包名?

2 个答案:

答案 0 :(得分:1)

AllAppsActivity.java

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<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) {
    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://javatechig.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<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(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

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

};

activity_main.xml中

<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="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

snippet_list_row.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="wrap_content" >

    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="3dp"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingLeft="5dp" >

        <TextView
            android:id="@+id/app_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/app_paackage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:1)

试试这个:

final PackageManager pm = getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
String app="";
for(PackageInfo pi : packages){
    if(!isSystemPackage(pi)){
app = pi.applicationInfo.loadLabel(getPackageManager()).toString();
Log.i("App name " , app);}
}    

 boolean isSystemPackage(PackageInfo pkgInfo) {
   return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
                : false;
    }  

尝试使用上面提到的isSystemPackage()查找非系统应用包。