android - 为什么这段代码无法正常运行?
大家讲道理
大家讲道理 2017-04-17 17:57:33
0
3
383

package com.example.archer.mobliesafe;

import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

import static android.R.id.list;
import static android.view.View.inflate;

public class CleanCacheActivity extends AppCompatActivity {

private PackageManager packageManager;
private List<CacheInfo> cacheInfos;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initUI();
}
private void initUI() {

    setContentView(R.layout.activity_clean_cache);

    listView = (ListView) findViewById(R.id.list_view_cache);
    cacheInfos = new ArrayList<>();
    packageManager = getPackageManager();

//当我们去拿数据的时候,面对的是一个耗时的操作,因此需要开启一个子线程

    new Thread(){
        @Override
        public void run() {
            //        packageManager.getPackageSizeInfo();
            List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);

            for (PackageInfo packageInfo : installedPackages) {
                getCacheSize(packageInfo);
            }

            handler.sendEmptyMessage(0);
        }

    }.start();

}

private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        CacheAdapter MyAdapter=new CacheAdapter();
        listView.setAdapter(MyAdapter);
    }
};

//写一个适配器用于适配数据
private class CacheAdapter extends BaseAdapter{

    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        return cacheInfos.size();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        return cacheInfos.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //设置缓存,提高效率
        ViewHolder  holder ;
        View view;
        if (convertView==null){
            //一定要初始化
            view = View.inflate(CleanCacheActivity.this, R.layout.item_clean_cache, null);
            System.out.println("======================================================");
            System.out.println("======================================================");
            System.out.println("======================================================");
            System.out.println("======================================================");
            System.out.println("======================================================");
            holder = new ViewHolder();
            holder.imageView= (ImageView) view.findViewById(R.id.iv_cache_icon);
            holder.tv_cache_size= (TextView) view.findViewById(R.id.tv_cache_size);
            holder.tv_cache_name= (TextView) view.findViewById(R.id.tv_cache_name);

            view.setTag(holder);

        }else {
            //通过tag找到缓存布局
            view =convertView;
            holder= (ViewHolder) view.getTag();

        }

        holder.imageView.setImageDrawable(cacheInfos.get(position).icon);
        holder.tv_cache_name.setText(cacheInfos.get(position).AppName);
        holder.tv_cache_size.setText("缓存:"+cacheInfos.get(position).cacheSize);
        return view;
    }

}
private void getCacheSize(PackageInfo  packageInfo) {

    try {

// Class<?> clazz = getClassLoader().loadClass("packageManager");

        //通过反射获取到当前的方法
        Method method = PackageManager.class.getDeclaredMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class);

        method.invoke(packageManager,packageInfo.applicationInfo.packageName,new  MyIPackageStatsObserver(packageInfo));

    } catch (Exception e) {
        e.printStackTrace();
    }
}
private class  MyIPackageStatsObserver extends  IPackageStatsObserver.Stub{
    private  PackageInfo packageInfo;

    MyIPackageStatsObserver(PackageInfo packageInfo) {
        this.packageInfo=packageInfo;
    }

    @Override
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {

        //获取到当前手机应用大小
        long cacheSize = pStats.cacheSize;
        //判断是否有缓存
        if (cacheSize>0){

            System.out.println("当前应用的名字:"+packageInfo.applicationInfo.loadLabel(packageManager)+
                    " 缓存的大小: "+cacheSize);
            CacheInfo cacheInfo = new CacheInfo();
            Drawable icon= packageInfo.applicationInfo.loadIcon(packageManager);
            cacheInfo.icon= icon;
            String appName= packageInfo.applicationInfo.loadLabel(packageManager).toString();
            cacheInfo.AppName = appName;

            cacheInfo.cacheSize=cacheSize;

            cacheInfos.add(cacheInfo);
        }

    }
}
//封装成一个对象

static  class CacheInfo{
    Drawable icon;
    long cacheSize;
    String AppName;
}

static  class ViewHolder{
    ImageView imageView;
    TextView tv_cache_name;
    TextView tv_cache_size;

}

}

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
迷茫

First determine whether it cannot run or an error is thrown. Please fill in the error content for the latter

黄舟

Debug to see if there is any data in your list

PHPzhong
  1. First standardize the code structure.

  2. If it cannot run normally, is it because an exception is thrown during runtime or the result cannot be displayed correctly?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template