android_ Foundation_ Get system application information

Reprinted from: https://blog.csdn.net/spinchao/article/details/92778861

1,demo

Sometimes, when we need to obtain the application of the system, such as the Home interface, we need to obtain the information of all applications registered in the Manifest, display the icons and names of these applications, and obtain their action or CompanentName, so that we can jump to the corresponding application when clicking them.

Here is an example:

Effect drawing from running on:

Obviously, this requires a ListView. Take a look at the layout file installed_app.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/installed_title"
	    android:textColor="@android:color/white"
	    android:textSize="24sp"/>
    
    <ListView
    	android:id="@+id/mylist"
    	android:layout_width="fill_parent"
    	android:layout_height="0dip"
    	android:layout_weight="1"/>
</LinearLayout>

We need a file to display the information of the application: for example, the name of the application, the icon, the clicked Intent, etc., as shown below:

package com.android.test;
 
import android.content.ComponentName;
import android.graphics.drawable.Drawable;
 
public class ApplicationInfo {
    
    String name;
    ComponentName intent;
    Drawable icon;
    
    public String getName () {
        return name;
    }
    
    public void setName (String name) {
        this.name = name;
    }
    
    public ComponentName getIntent () {
        return intent;
    }
    
    public void setIntent (ComponentName intent) {
        this.intent = intent;
    }
    
    public Drawable getIcon () {
        return icon;
    }
    
    public void setIcon (Drawable icon) {
        this.icon = icon;
    }
}

Well, now we're going to get the application information and package the obtained information in a List. The details are as follows:

private List<ApplicationInfo> loadAppInfomation(Context context) {
        List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>();
        PackageManager pm = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        Collections.sort(infos, new ResolveInfo.DisplayNameComparator(pm));
        if(infos != null) {
            apps.clear();
            for(int i=0; i<infos.size(); i++) {
                ApplicationInfo app = new ApplicationInfo();
                ResolveInfo info = infos.get(i);
                app.setName(info.loadLabel(pm).toString());
                app.setIcon(info.loadIcon(pm));
                app.setIntent(new ComponentName(info.activityInfo.packageName, info.activityInfo.name));
                apps.add(app);
            }
        }
        return apps;
    }

First, get the package manager, and then scan with Action_main and Category_Launcher's Intent_filter. The returned value of the query is a List, and each ResolveInfo in it is the information of an application.

Then we need to traverse the List to get the relevant information of each application (the information we need), then put the information we need in an ApplicationInfo object, and then put the ApplicationInfo of all applications into a List, which is the data we need.

Then we need to write an Adapter to pass in the data and let the ListView display what we need to display:

The Adapter is as follows:

package com.android.test;
 
import java.util.List;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class ApplicationAdapter extends BaseAdapter {
    
    private List<ApplicationInfo> apps;
    private LayoutInflater inflater;
    
    public ApplicationAdapter (Context context, List<ApplicationInfo> infos) {
        this.apps = infos;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount () {
        return apps.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 holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.app_adapter_list_item, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
            holder.name = (TextView) convertView.findViewById(R.id.app_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.icon.setImageDrawable(apps.get(position).getIcon());
        holder.name.setText(apps.get(position).getName());
        return convertView;
    }
    
    class ViewHolder {
        ImageView icon;
        TextView name;
    }
}

In the Adapter, each item item is displayed in the getView. I won't talk too much here. Here, we need to display the icon and name of the application. Therefore, in the layout, we need an ImageView and a TextView. The layout file app of the item item of the Adapter_ Adapter_ list_ item. The XML is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
  <ImageView
  	android:id="@+id/app_icon"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"/>
  <TextView
  	android:id="@+id/app_name"
  	android:layout_width="0dip"
  	android:layout_weight="1"
  	android:gravity="center"
  	android:textColor="@android:color/white"
  	android:textSize="20sp"
  	android:layout_height="wrap_content"/>	
</LinearLayout>

Here, the entire ListView can be displayed. We also need to write click events:

@Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setComponent(apps.get(position).getIntent());
        startActivity(intent);
    }

Let's release the main Activity:

package com.android.test;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
 
public class GetApplicationOfSystem extends Activity implements OnItemClickListener {
    
    private ListView mListView;
    private ApplicationAdapter mAdapter;
    private List<ApplicationInfo> apps;
    
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_app);
        
        mListView = (ListView) findViewById(R.id.mylist);
        mListView.setOnItemClickListener(this);
        apps = loadAppInfomation(this);
        mAdapter = new ApplicationAdapter(this, apps);
        mListView.setAdapter(mAdapter);
    }
    
    private List<ApplicationInfo> loadAppInfomation(Context context) {
        List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>();
        PackageManager pm = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        Collections.sort(infos, new ResolveInfo.DisplayNameComparator(pm));
        if(infos != null) {
            apps.clear();
            for(int i=0; i<infos.size(); i++) {
                ApplicationInfo app = new ApplicationInfo();
                ResolveInfo info = infos.get(i);
                app.setName(info.loadLabel(pm).toString());
                app.setIcon(info.loadIcon(pm));
                app.setIntent(new ComponentName(info.activityInfo.packageName, info.activityInfo.name));
                apps.add(app);
            }
        }
        return apps;
    }
 
    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setComponent(apps.get(position).getIntent());
        startActivity(intent);
    }
}

In another case, we sometimes need to obtain the information of the installed applications rather than the application information of the whole system,

This is to obtain the information of the installed application in another way:

private List<PackageInfo> loadPackageInfo(Context context) {
        List<PackageInfo> apps = new ArrayList<PackageInfo>();
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packageList = pm.getInstalledPackages(0);
        for(int i=0; i<packageList.size(); i++) {
            PackageInfo info = packageList.get(i);
            if((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) {
                apps.add(info);
            }
        }
        return apps;
    }

The installed packages can be obtained from the local packages. Note that the installed packages here refer to some packages, including system packages. We only need to install them ourselves, and we don't need the system. What should we do? At this time, we need to filter out the system packages, and we can make a judgment if ((info. Applicationinfo. Flags & Info. Application. Flag_system) < = 0), It means it's not a system package. It was installed later. We can add it to the List.

Get the installed package's Adapter:

package com.android.test;
 
import java.util.List;
 
import android.content.Context;
import android.content.pm.PackageInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class InstalledPackageAdapter extends BaseAdapter {
    
    private List<PackageInfo> mApps;
    private LayoutInflater inflater;
    private Context mContext;
    
    public InstalledPackageAdapter (Context context, List<PackageInfo> infos) {
        this.mContext = context;
        this.mApps = infos;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount () {
        return mApps.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 holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.app_adapter_list_item, null);
            holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
            holder.name = (TextView) convertView.findViewById(R.id.app_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.icon.setImageDrawable(mApps.get(position).applicationInfo.loadIcon(mContext.getPackageManager()));
        holder.name.setText(mApps.get(position).applicationInfo.loadLabel(mContext.getPackageManager()));
        return convertView;
    }
    
    class ViewHolder {
        ImageView icon;
        TextView name;
    }
}

Get the Activity of the installed app:

package com.android.test;
 
import java.util.ArrayList;
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
 
public class GetApplicationOfInstalled extends Activity implements OnItemClickListener {
    
    private ListView mListView;
    private InstalledPackageAdapter maAdapter;
    private List<PackageInfo> mApps;
    
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.installed_app);
        mListView = (ListView) findViewById(R.id.mylist);
        mListView.setOnItemClickListener(this);
        mApps = loadPackageInfo(this);
        maAdapter = new InstalledPackageAdapter(this, mApps);
        mListView.setAdapter(maAdapter);
    }
    
    private List<PackageInfo> loadPackageInfo(Context context) {
        List<PackageInfo> apps = new ArrayList<PackageInfo>();
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packageList = pm.getInstalledPackages(0);
        for(int i=0; i<packageList.size(); i++) {
            PackageInfo info = packageList.get(i);
            if((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) {
                apps.add(info);
            }
        }
        return apps;
    }
 
    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        PackageInfo packageInfo = mApps.get(position);
        startActivity(intent);
    }
}

Get the effect picture of the installed application:

Transferred from: http://www.ideaex.net/html/Article/2011/08/27/520.html

Source download

2. android label label placeholder to get app name

Mode 1

    android:label="@string/app_name" 
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);        

Mode 2 ("recommended")

In case of placeholder adoption

   android:label="${xxx}",use loadlabel,Compatible with the above without placeholders
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.applicationInfo.loadLabel(packageManager).toString();

Keywords: Android

Added by jeeva on Wed, 19 Jan 2022 19:02:18 +0200