获取系统内安装程序列表

来源:互联网 发布:写简谱的软件 编辑:程序博客网 时间:2024/06/05 16:35

原文地址:http://blog.csdn.net/qq_16131393/article/details/51367597


实现功能:

获取手机应用图标,名称,时间(安装时间/更新时间),大小,侧滑删除应用,点击应用图标分享等功能。

目标效果:

这里写图片描述

思路:RecylerView+swipereveallayout

贴上dependencies

这里写图片描述

介绍:Glide实现图片加载,EventBus通信,swipereveallayout实现侧滑。

获取数据源:

   private ArrayList<AppInfoModel> getData() {        ArrayList<AppInfoModel> list = new ArrayList<>();        PackageManager mPackageManager = getApplicationContext().getPackageManager();        packageInfoList = (ArrayList<PackageInfo>) mPackageManager.getInstalledPackages(0);        for (PackageInfo packageInfo : packageInfoList) {            AppInfoModel model = new AppInfoModel();            model.setName(getApplicationName(packageInfo.applicationInfo.packageName, mPackageManager));            model.setIcon(getAppliactionIcon(packageInfo, mPackageManager));            model.setTime(getDate(packageInfo.lastUpdateTime));            model.setSize(new File(packageInfo.applicationInfo.sourceDir).length() / 1024 / 1024 + "MB");            list.add(model);        }        return list;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

获取应用名:

/**     * 获取应用的名称     */    public String getApplicationName(String packageName, PackageManager packageManager) {        String applicationName = null;        try {            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);            applicationName = (String) packageManager.getApplicationLabel(applicationInfo);        } catch (PackageManager.NameNotFoundException e) {        }        return applicationName;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

获取应用的Icon

 /**     * 获取应用的Icon     */    public Drawable getAppliactionIcon(PackageInfo packageInfo, PackageManager packageManager) {        Drawable appliactionIcon = packageInfo.applicationInfo.loadIcon(packageManager);        return appliactionIcon;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

生成时间

/**     * 生成时间     */    public static String getDate(long time) {        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = new Date(time);        String formatedDate = simpleDateFormat.format(date);        return formatedDate;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

EventBus实现通信

    //事件3接收者:在主线程接收    public void onEventMainThread(ClickEvent event) {        switch (event.type) {            case ClickEvent.CLICK_ITEM:                int position = (int) event.data;                File apkFile = new File(packageInfoList.get(position).applicationInfo.sourceDir);                Intent intent = new Intent();                intent.setAction(Intent.ACTION_SEND);                intent.setType("*/*");                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkFile));                startActivity(intent);                break;        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

适配器:

package com.example.wangchang.testqqfile.adapter;import android.os.Bundle;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.chauthai.swipereveallayout.SwipeRevealLayout;import com.chauthai.swipereveallayout.ViewBinderHelper;import com.example.wangchang.testqqfile.R;import com.example.wangchang.testqqfile.bean.AppInfoModel;import com.example.wangchang.testqqfile.event.ClickEvent;import java.util.ArrayList;import de.greenrobot.event.EventBus;/** * Created by WangChang on 2016/5/7. */public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoViewHolder> {    private ArrayList<AppInfoModel> list = new ArrayList<>();    private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();    public void getData(ArrayList<AppInfoModel> data) {        list.clear();        if (data != null) {            list.addAll(data);        }        notifyDataSetChanged();    }    @Override    public DemoAdapter.DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        return new DemoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));    }    @Override    public void onBindViewHolder(DemoAdapter.DemoViewHolder holder, int position) {        holder.setData(list.get(position));    }    public void saveStates(Bundle outState) {        viewBinderHelper.saveStates(outState);    }    public void restoreStates(Bundle inState) {        viewBinderHelper.restoreStates(inState);    }    @Override    public int getItemCount() {        return list != null ? list.size() : 0;    }    public class DemoViewHolder extends RecyclerView.ViewHolder {        private TextView name, time, size;        private ImageView icon;        private SwipeRevealLayout swipeRevealLayout;        private View deleteLayout;        public DemoViewHolder(View itemView) {            super(itemView);            name = (TextView) itemView.findViewById(R.id.name);            time = (TextView) itemView.findViewById(R.id.time);            size = (TextView) itemView.findViewById(R.id.size);            icon = (ImageView) itemView.findViewById(R.id.icon);            swipeRevealLayout= (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout);            deleteLayout = itemView.findViewById(R.id.delete_layout);            icon.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    EventBus.getDefault().post(new ClickEvent(ClickEvent.CLICK_ITEM, getAdapterPosition()));                }            });            deleteLayout.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    list.remove(getAdapterPosition());                    notifyItemRemoved(getAdapterPosition());                }            });        }        void setData(AppInfoModel model) {            if (model != null) {                viewBinderHelper.bind(swipeRevealLayout,model.getName());                name.setText(model.getName());                time.setText(model.getTime());                size.setText(model.getSize());                icon.setImageDrawable(model.getIcon());            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

item布局,包含侧滑布局

<?xml version="1.0" encoding="utf-8"?><com.chauthai.swipereveallayout.SwipeRevealLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/swipe_layout"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="10dp"    app:dragEdge="right"    app:mode="same_level">    <FrameLayout        android:id="@+id/delete_layout"        android:layout_width="wrap_content"        android:layout_height="70dp"        android:background="#ffcc0000">        <TextView            android:layout_width="70dp"            android:layout_height="match_parent"            android:background="@android:color/holo_red_dark"            android:gravity="center"            android:text="Delete"            android:textColor="@android:color/white" />    </FrameLayout>    <FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            android:padding="10dp">            <LinearLayout                android:layout_width="match_parent"                android:layout_height="70dp"                android:orientation="horizontal">                <ImageView                    android:id="@+id/icon"                    android:layout_width="50dp"                    android:layout_height="50dp"                    android:src="@mipmap/ic_launcher" />                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_marginLeft="24dp"                    android:layout_toRightOf="@+id/icon"                    android:orientation="vertical">                    <TextView                        android:id="@+id/name"                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:ellipsize="middle"                        android:singleLine="true"                        android:textColor="@android:color/black"                        android:textStyle="bold" />                    <TextView                        android:id="@+id/size"                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:layout_marginTop="5dp"                        android:textSize="10sp" />                    <TextView                        android:id="@+id/time"                        android:layout_width="match_parent"                        android:layout_height="wrap_content"                        android:layout_marginTop="5dp"                        android:textColor="#aaaaaa"                        android:textSize="10sp" />                </LinearLayout>            </LinearLayout>            <View                android:layout_width="match_parent"                android:layout_height="0.5dp"                android:background="#E0E0E0" />        </LinearLayout>    </FrameLayout></com.chauthai.swipereveallayout.SwipeRevealLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

具体请参考:

https://github.com/chthai64/SwipeRevealLayout

实现效果:

这里写图片描述

侧滑效果:

这里写图片描述

分享效果:

这里写图片描述

效果就到这,源码下载地址

http://pan.baidu.com/s/1gftwfon

0 0
原创粉丝点击