android - 为什么这段代码无法正常运行?

浏览:50日期:2022-11-16

问题描述

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;@Overrideprotected 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(){@Overridepublic 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;}

}

问题解答

回答1:

先确定是无法运行还是抛出错误,后者请填出错误内容

回答2:

debug,看一下你的列表到底有没有数据

回答3:

先规范一下代码结构。

不能正常运行是运行时抛出异常还是结果无法正确显示?

相关文章: