Android获取应用程序列表(并点击可以跳转进入)

应用程序由3部分组成:应用在手机中存储的包名,应用名,应用图标。

为了方便,创建APPInfo类把这三条信息封装:

APPInfo.class:

import android.graphics.drawable.Drawable;
import android.widget.Button;

public class AppInfo  {

    public Drawable mappimage = null;
    public String mappname = "";
    public String mpackagename = "";

    //构造方法
    public AppInfo(Drawable appimage,String appname,String packagename){
        this.mappimage=appimage;
        this.mappname=appname;
        this.mpackagename=packagename;
    }

}

然后获取应用程序的信息,这要借助包管理器PackageManager进行:

//获取app信息
public void getAppProcessName(Context context) {

     final PackageManager packageManager = getPackageManager();
     final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
     mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

     final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);

     for (int i = 0; i < apps.size(); i++) {
         Drawable imageicon = apps.get(i).activityInfo.loadIcon(packageManager); //获得图标(此处得到的为Drawable型的,xml文件中的图片是Imageview型的,需要用setImageDrawable进行转换,见MyAdapter.class)
         String appname = apps.get(i).activityInfo.applicationInfo.loadLabel(packageManager).toString(); //获得名称
         String packagname = apps.get(i).activityInfo.packageName; //获得包名
         AppInfo appinfo = new AppInfo(imageicon, appname, packagname);  //创建appinfo对象
         applist.add(appinfo);  //加入应用信息队列
         Log.i("NAME", "getAppProcessName: " + appname);//打印日志查看
     }
 }

应用信息用列表ListView来保存,每一项显示一个应用的图标,名称,包名和一个点击可跳转的按钮。见前一篇ListView图文列表可知需要一个适配器来规定ListView中每一项的内容布局。点击按钮要实现跳转,需要添加监听器。获得包名是很重要的,跳转时用包名来指出要跳转到什么地方
适配器可以看成ListView的定义,跳转的监听同样要写在适配器中

MyAdapter.class:

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

//适配器类
public class MyAdapter extends ArrayAdapter<AppInfo> {

    private Context mContext;
    private int mresource;
    private List<AppInfo> list;
    private Activity selectmain;

    public MyAdapter(Context context,int resource,List<AppInfo> data,Activity selectmain){
        super(context,resource,data);
        this.mContext=context;
        this.mresource=resource;
        this.list=data;
        this.selectmain=selectmain;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(mContext).inflate(mresource,null);
            holder.image=(ImageView) convertView.findViewById(R.id.appimage);
            holder.title=(TextView) convertView.findViewById(R.id.appname);
            holder.text=(TextView)convertView.findViewById(R.id.packagname);
            holder.button=(Button)convertView.findViewById(R.id.appbutton);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }

        holder.image.setImageDrawable(getItem(position).mappimage);
        holder.title.setText(getItem(position).mappname);
        final String m_appname = getItem(position).mappname;
        holder.text.setText(getItem(position).mpackagename);
        final String m_packagename = getItem(position).mpackagename;
        final int appcpu = getAppCpuUsedPercent(getItem(position).mpackagename);
        holder.button.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Toast.makeText(mContext,"已选择: "+m_appname,Toast.LENGTH_SHORT).show();
                     int nowpid = android.os.Process.myPid();//得到正在运行的应用的pid
                     Log.e("TagButton","当前应用消耗率:"+appcpu);
                     launch(m_packagename);
                 }
             }

        );

        return convertView;
    }

    public class ViewHolder{
        ImageView image;
        TextView title;
        TextView text;
        Button button;
    }

    //跳转进入应用
    private void launch(String packagename) {
        Intent intent = selectmain.getPackageManager().getLaunchIntentForPackage(packagename);
        //非空跳转
        if (intent != null) {
            selectmain.startActivity(intent);
        } else {
            // 没有安装要跳转的应用
            Toast.makeText(selectmain.getApplicationContext(), "没有找到app", Toast.LENGTH_LONG).show();
        }
    }

}

最后只需要在Activity中找到ListView并给它加上适配器即可:

ListView listView=(ListView)findViewById(R.id.applist);  //找到列表
MyAdapter arrayAdapter=new MyAdapter(this,R.layout.applistform,applist,SelectActivity.this);  //定义适配器
listView.setAdapter(arrayAdapter);  //添加适配器

以上参考了网上很多代码

代码全部完整

仍在入门,如有错误,欢迎指出

(0)

相关推荐