通过PackageManager启动/卸载已安装的应用

通过PackageManager启动/卸载已安装的应用_第1张图片

AppInfo类

package example.com.viewtest.myview;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by yzr on 16-2-5.
 */
public class AppInfo {
    private  String activityName;
    private  String pkgName;
    private  String label;
    private Drawable drawable;

    public Drawable getDrawable() {
        return drawable;
    }

    public String getActivityName() {
        return activityName;
    }

    public String getLabel() {
        return label;
    }

    public String getPkgName() {
        return pkgName;
    }

    public Intent getIntent(){
        Intent intent=new Intent();
        intent.setComponent(new ComponentName(pkgName, activityName));
        return  intent;
    }
   public   AppInfo(String activityName,String pkgName,String appLabel,Drawable drawable){
        this.activityName=activityName;
        this.pkgName=pkgName;
        this.label=appLabel;
        this.drawable=drawable;
    }

    @Override
    public String toString() {
        return  pkgName+"/"+activityName+":"+label;
    }

    public  static  ArrayList<AppInfo>getAppInfoList(PackageManager manager){

        Intent intent=new Intent(Intent.ACTION_MAIN,null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos=manager.queryIntentActivities(intent,PackageManager.MATCH_ALL);
        Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(manager));

        ArrayList<AppInfo>appInfoArrayList=new ArrayList<>();

        for(ResolveInfo info :resolveInfos){
            String activityName=info.activityInfo.name;
            String pkgName=info.activityInfo.packageName;
            String appLabel=(String)info.loadLabel(manager);
            Drawable drawable=info.loadIcon(manager);
            AppInfo appInfo=new AppInfo(activityName,pkgName,appLabel,drawable);
            appInfoArrayList.add(appInfo);
        }
        return appInfoArrayList;
    }

}

AppAdapter
package example.com.viewtest.myview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import example.com.viewtest.R;

/**
 * Created by yzr on 16-2-5.
 */
public class AppAdapter extends ArrayAdapter <AppInfo>{


    class  ViewHolder
    {
        ImageView icno;
        TextView appName;
    }
    int resourceId;

    public AppAdapter(Context context, int resource, List<AppInfo> objects) {
        super(context, resource, objects);
        this.resourceId=resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        AppInfo info=getItem(position);
        View view;
        ViewHolder holder;
        if(convertView==null)
        {
            view= LayoutInflater.from(getContext()).inflate(resourceId,null);
            holder=new ViewHolder();
            holder.icno=(ImageView)view.findViewById(R.id.app_icon);
            holder.appName=(TextView)view.findViewById(R.id.app_label);
            view.setTag(holder);
        }
        else
        {
            view=convertView;
            holder=(ViewHolder)view.getTag();
        }
        holder.icno.setImageDrawable(info.getDrawable());
        holder.appName.setText(info.getLabel());

       return  view;
    }
}

package example.com.viewtest.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import example.com.viewtest.R;
import example.com.viewtest.myview.AppAdapter;
import example.com.viewtest.myview.AppInfo;
import example.com.viewtest.service.LockService;

public class AppInfoActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_fresco);
        final PackageManager manager=getPackageManager();
        List<AppInfo>list=AppInfo.getAppInfoList(manager);
        ListView listView=(ListView)findViewById(R.id.list);
        AppAdapter adapter=new AppAdapter(this,R.layout.app_item,list);
        listView.setAdapter(adapter);
        
        //点击启动应用
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AppInfo info = (AppInfo) parent.getItemAtPosition(position);
                Intent intent = info.getIntent();
                startActivity(intent);
            }
        });

        //长按删除应用
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AppInfo info = (AppInfo) parent.getItemAtPosition(position);
                Uri uri = Uri.parse("package:" + info.getPkgName());
                Intent intent = new Intent(Intent.ACTION_DELETE);
                intent.setData(uri);
                startActivity(intent);
                return true;
            }
        });
    }

}
删除应用需要添加权限:      <uses-permission android:name="android.permission.DELETE_PACKAGES" />



你可能感兴趣的:(通过PackageManager启动/卸载已安装的应用)