简体   繁体   中英

{ need to show in a list view only the installed applications that uses the internet connection

I need to show all the applications that uses the internet connection either WiFi or 4G. I have reached to list all the applications but I don't know how to filter it.

I've seen a question like that before but no solutions I have tried where they've achieved but not working.

private List<AppList> getAllInstalledApps() {

        List<AppList> List = new ArrayList<AppList>();
        List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);

            if ((isSystemPackage(p) == false)) {
                String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
                Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
                List.add(new AppList(appName, icon));

            }
        }
        return List;
    }

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

    public class AppAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;
        private List<AppList> appInList;

        public AppAdapter(Context context, List<AppList> customizedListView) {
            layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            appInList = customizedListView;
        }
        @Override
        public int getCount() {
            return appInList.size();
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder listViewHolder;
            if(convertView == null){
                listViewHolder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.app_item_layout, parent, false);

                listViewHolder.appNameText = (TextView)convertView.findViewById(R.id.list_app_name);
                listViewHolder.appIcon = (ImageView)convertView.findViewById(R.id.app_icon);
                convertView.setTag(listViewHolder);
            }
            else{
                listViewHolder = (ViewHolder)convertView.getTag();
            }
            listViewHolder.appNameText.setText(appInList.get(position).getName());
            listViewHolder.appIcon.setImageDrawable(appInList.get(position).getIcon());

            return convertView;
        }
    }
    static class ViewHolder{
        TextView appNameText;
        ImageView appIcon;
    }

    public class AppList {

        private String name;
        Drawable icon;

        public AppList(String name, Drawable icon) {
            this.name = name;
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public Drawable getIcon() {
            return icon;
        }
    }

I expect it to show all the applications installed on the device that uses internet connection.

Your current code only allows you to fetch all installed apps and filter for System apps.

If you want to filter for apps that uses an Internet connection, then you'll have to check for the permission: android.permission.INTERNET .

For that, you can't simply use:

List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

You'll have to use:

List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);

Including this flag allows you to look into the permissions that are used for the apps you fetch.

Afterwards, similar to what you're currently doing, you'll have to iterate through each of the PackageInfo items and check if the INTERNET permission is there.

private boolean usesInternet(PackageInfo pInfo) {
    if(pInfo.requestedPermissions != null) {
        for(String i : pInfo.requestedPermissions)
            if(i.equals(Manifest.permission.INTERNET)
                return true;
    }
    return false;
}

Finally, you can add use this method in your filtering if statement, like so:

for(PackageInfo p : packs){
    if(!isSystemPackage(p)){
        if(usesInternet(p)){
            String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
            Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
            List.add(new AppList(appName, icon));
        }
    }
}

With this, you'll be first checking to see if the app is a System app or not. If it's not, then it'll check if the INTERNET permission is one of the declared permissions within the app. If it is, then you add that app into your List.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM