android实用方法收集

这篇博客汇总了Android开发中的多个实用方法,包括获取状态栏高度、设置无滑动ListView、检测SD卡、设置壁纸、获取应用列表等,涵盖了设备信息获取、UI操作和文件管理等多个方面。
摘要由CSDN通过智能技术生成

1.获取状态栏的高度

/**
	 * 用于获取状态栏的高度。
	 * 
	 * @return 返回状态栏高度的像素值。
	 */
	private int getStatusBarHeight() {
		if (statusBarHeight == 0) {
			try {
				Class<?> c = Class.forName("com.android.internal.R$dimen");
				Object o = c.newInstance();
				Field field = c.getField("status_bar_height");
				int x = (Integer) field.get(o);
				statusBarHeight = getResources().getDimensionPixelSize(x);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return statusBarHeight;
	}


2.设置listview的高度,使其无滑动效果

	/**
	 * 根据listitem的数量来listview确定高度
	 * 使listview无需滑动
	 * @param listView
	 */
	private void setListViewHeightBasedOnChildren(ListView listView) 
	{
		ListAdapter listAdapter = (ListAdapter) listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) 
        {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }


3.得到设备所有内存

		 	/**
			 * 得到设备的所有RAM
			 * @return 返回所有内存大小,单位:kb
			 */
			private int getAllMemory() {
				String filePath = "/proc/meminfo";
				int ram = 0;
				FileReader fr = null;
				BufferedReader localBufferedReader = null;
				try {
					fr = new FileReader(filePath);
					localBufferedReader = new BufferedReader(fr, 8192);
					String line = localBufferedReader.readLine();
					int a = line.length() - 3;
					int b = line.indexOf(' ');
					String str = line.substring(b, a);
					while (str.substring(0, 1).equals(" ")) {
						str = str.substring(1, str.length());
					}
					ram = Integer.parseInt(str);
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						fr.close();
						localBufferedReader.close();
					} catch (Exception e) {
			
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值