Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習之獲取系統應用信息列表的實現

Android學習之獲取系統應用信息列表的實現

編輯:關於Android編程

前言:

好幾天電腦打不開CSDN博客,也不知道怎麼回事,今天下班回來突然能打開了,遂將周末實現的一個效果貼上。

實現功能:

獲取手機應用圖標,名稱,時間(安裝時間/更新時間),大小,側滑刪除應用,點擊應用圖標分享等功能。

目標效果:

這裡寫圖片描述

思路:RecylerView+swipereveallayout

貼上dependencies<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxpbWcgYWx0PQ=="這裡寫圖片描述" src="/uploadfile/Collfiles/20160511/20160511090503202.png" title="\" />

介紹:Glide實現圖片加載,EventBus通信,swipereveallayout實現側滑。

獲取數據源:

   private ArrayList getData() {
        ArrayList list = new ArrayList<>();
        PackageManager mPackageManager = getApplicationContext().getPackageManager();
        packageInfoList = (ArrayList) 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;
    }

獲取應用名:

/**
     * 獲取應用的名稱
     */
    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;
    }

獲取應用的Icon

 /**
     * 獲取應用的Icon
     */
    public Drawable getAppliactionIcon(PackageInfo packageInfo, PackageManager packageManager) {
        Drawable appliactionIcon = packageInfo.applicationInfo.loadIcon(packageManager);
        return appliactionIcon;
    }

生成時間

/**
     * 生成時間
     */
    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;
    }

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;
        }
    }

適配器:

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 {

    private ArrayList list = new ArrayList<>();
    private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();


    public void getData(ArrayList 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());

            }
        }
    }
}

item布局,包含側滑布局

<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">
    </textview></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">
                </textview></textview></textview></linearlayout>
            </imageview></linearlayout>

            <view android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#E0E0E0">
        </view></linearlayout>
    </framelayout>
</com.chauthai.swipereveallayout.swipereveallayout>
</code>

具體請參考:

https://github.com/chthai64/SwipeRevealLayout

實現效果:

這裡寫圖片描述

側滑效果:

這裡寫圖片描述

分享效果:

這裡寫圖片描述

效果就到這,源碼下載地址

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved