get Installed Apps - Android App

Android examples for App:App Install

Description

get Installed Apps

Demo Code

/*******************************************************************************
 * Copyright 2013 momock.com//from  w ww  .java  2  s  . com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {
    public static PackageInfo[] getInstalledApps(Context context) {
        PackageManager pkgManager = context.getPackageManager();
        List<PackageInfo> packs = pkgManager.getInstalledPackages(0);
        List<PackageInfo> appList = new ArrayList<PackageInfo>();
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            Intent intent = pkgManager
                    .getLaunchIntentForPackage(p.packageName);
            if (intent != null) {
                appList.add(p);
            }
        }
        PackageInfo[] apps = new PackageInfo[appList.size()];
        appList.toArray(apps);
        Arrays.sort(apps, new Comparator<PackageInfo>() {

            @Override
            public int compare(PackageInfo lhs, PackageInfo rhs) {
                return lhs.packageName.compareToIgnoreCase(rhs.packageName);
            }

        });
        return apps;
    }
}

Related Tutorials