各种小记录

16 篇文章 0 订阅

Rect rect = new Rect();
getDrawingRect(rect);
int height = rect.bottom - rect.top;

1、sqlite中处理单引号:

所有单引号换成双单引号,如:

content.replace("'", "''");

这样是不行的,临时抱佛脚,换成了带"?"的通配形式


2、SimpleCursorAdapter 的 notifyDataSetChanged无效:

可以使用SimpleCursorAdapter 的changeCursor方法:

http://stackoverflow.com/questions/1985955/android-simplecursoradapter-not-updating-with-database-changes

http://stackoverflow.com/questions/14034770/using-notifydatasetchanged-on-simplecursoradapter-does-not-work


3、listview不显示分割线:

setDriver(null),或者在xml文件中属性设置为@null


4、sqlite 中replace into 无效:

之前写好的代码调试都通过了,今天突然发现不能运行了:sqlite,表中创建唯一索引,然后使用replace into实现插入或者更新,结果变成了只插入,折腾了几个小时,最后改掉了表中一个名为id的字段,实在是无语


5、临时写java的Thread封装:

package com.ttdevs.thread;

import android.os.Process;

public abstract class BaseThread extends Thread {

	private volatile boolean mQuit = false;

	public BaseThread() {

	}

	/**
	 * prepare if needed
	 */
	public boolean prepare() {

		return true;
	}

	/**
	 * run body
	 */
	public abstract void execute() throws Exception; // third modify,second modify maybe not neccessary

	/**
	 * recycle if needed
	 */
	public void recycle() {

	}

	/**
	 * stop thread
	 */
	public final void quit() {
		mQuit = true;
		interrupt();
	}

	@Override
	public void run() {
		// TODO
		Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
		if (!prepare()) {
			return;
		}
		while (true) {
			if (mQuit) { // second modify
				recycle();
				return;
			}
			try {
				execute();
			} catch (Exception e) {// } catch (InterruptedException e) {
				if (mQuit) {
					recycle();
					return;
				}
				continue;
			}
		}
	}
}

6、volley中设置连接超时:

设置超时的地方:
HurlStack:
    /**
     * Opens an {@link HttpURLConnection} with parameters.
     * @param url
     * @return an open connection
     * @throws IOException
     */
    private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
        HttpURLConnection connection = createConnection(url);

        int timeoutMs = request.getTimeoutMs();
        connection.setConnectTimeout(timeoutMs);
        connection.setReadTimeout(timeoutMs);
        connection.setUseCaches(false);
        connection.setDoInput(true);

        // use caller-provided custom SslSocketFactory, if any, for HTTPS
        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
        }

        return connection;
    }
HttpClientStack:
    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {
        HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
        addHeaders(httpRequest, additionalHeaders);
        addHeaders(httpRequest, request.getHeaders());
        onPrepareRequest(httpRequest);
        HttpParams httpParams = httpRequest.getParams();
        int timeoutMs = request.getTimeoutMs();
        // TODO: Reevaluate this connection timeout based on more wide-scale
        // data collection and possibly different for wifi vs. 3G.
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
        return mClient.execute(httpRequest);
    }
Request中:
/**
     * Sets the retry policy for this request.
     */
    public void setRetryPolicy(RetryPolicy retryPolicy) {
        mRetryPolicy = retryPolicy;
    }
RetryPolicy :
/**
 * Retry policy for a request.
 */
public interface RetryPolicy {

    /**
     * Returns the current timeout (used for logging).
     */
    public int getCurrentTimeout();

    /**
     * Returns the current retry count (used for logging).
     */
    public int getCurrentRetryCount();

    /**
     * Prepares for the next retry by applying a backoff to the timeout.
     * @param error The error code of the last attempt.
     * @throws VolleyError In the event that the retry could not be performed (for example if we
     * ran out of attempts), the passed in error is thrown.
     */
    public void retry(VolleyError error) throws VolleyError;
}

7、android 与图片相关:


8、将eclipse项目中的文件夹移除SVN的控制(添加到svn:ignore)

以删除android项目中的bin文件夹为例
a、关闭项目的自动build:project-Build Automatically
b、删除bin文件夹
c、提交
d、打开 项目的自动build,这个时候会生成bin文件夹
e、将bin添加到svn:ignore:右键-Team-添加至svn:ignore
f、提交

9、屏幕旋转,activity的成员变量被全部释放?!

貌似是这样子的

10、ViewPager频繁被回收

ViewPager
public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

Parameters
limit How many pages will be kept offscreen in an idle state.


11、android SQLite 分页查询,参数为页大小和起始编号

先看数据:


select * from china_city where province = '北京' order by code limit 5 offset 0 ,查询第0条之后的五条:


select * from china_city where province = '北京' order by code limit 5 offset 5,查询第5条之后的五条:


12、获取进程内存:

int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

13、与联系人相关:


14、设置字体大小:

这里可以用setTextSize()的另外一种形式,可以指定单位:
setTextSize(int unit, int size)
TypedValue.COMPLEX_UNIT_PX : Pixels
TypedValue.COMPLEX_UNIT_SP : Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels

15、交换两个变量的值

date3 = date1;
date1 = date2;
date2 = date3;

if (date2 > date1) {
	date2 = date2 + date1;
	date1 = date2 - date1;
	date2 = date2 - date1;
}

date2 = date1 ^ date2;
date1 = date1 ^ date2;
date2 = date1 ^ date2;

16、获取URI真实的地址

	private String getRealPath(Uri uri) {
		String path = "";
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
		}
		return path;
	}


17、自定义Gridview,解决ScrollView中嵌套Gridview显示不正常的问题

网上到处都可以找到下面的解决办法,但是这样会有一个问题:当图片非常多的时候是一次加载完毕的,而我们希望的应该是本来的GridView可以通过滑动判断当前显示条目的情况,在此记录一下。
/**
 * 自定义Gridview,解决ScrollView中嵌套Gridview显示不正常的问题<br>
 * 但是,这又产生了一个新的问题:所有的item都是一次加载完毕的,再想滑动加载就不行了
 * 
 * @author ttworking
 * 
 */
public class ImageGridView extends GridView {
	public ImageGridView(Context context) {
		this(context, null);
	}

	public ImageGridView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ImageGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
}


18、.9图片的制作

一个图片只想横向拉伸。所以就想当然的只在左边画了一条横线,运行的时候怎么都是有黑线,没拉伸。最后解决:给左边也加一条竖线!


20、listview点击背景不变

这个应该还是焦点的问题,在item的view最上层布局中添加一个属性:
android:descendantFocusability="blocksDescendants"


21、Volley Cookies

http://stackoverflow.com/questions/16680701/using-cookies-with-android-volley-library


22、EditText设置readOnley

et.setCursorVisible(false); 
et.setFocusable(false); 
et.setFocusableInTouchMode(false);


23、保存对象到SharedPreferences

想保存一个List<JavaBean> 到SharedPreferences。网上找了一圈有一种说法利用set,或者base64,都感觉不好。朋友一句提示:json。然后豁然开朗。现转化成json,然后保存,效果不错


24、获取字符

	public String getCharacter(String str) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < str.length(); i++) {
			if (String.valueOf(str.charAt(i)).getBytes().length > 1) {
				sb.append(str.charAt(i));
			}
		}
		return sb.toString();
	}

25、android 4 按钮样式

style="?android:attr/buttonBarButtonStyle"


26、几个sqlite的sql语句

select count(*) from notes

create table notes_new as select * from notes;

select count(*) from notes_new

delete from notes_new where word like '% %';
delete from notes_new where word like '%-%';
delete from notes_new where phonetic like '';

create table notes_new_new as select * from notes_new;

select count(*) from notes_new_new

delete from notes_new_new where _id not in (select min(_id) from notes_new_new group by word)

select * from notes_new_new

CREATE TABLE notes_word(
  _id INTEGER PRIMARY KEY AUTOINCREMENT,
  word TEXT,
  detail TEXT,
  created INT,
  phonetic TEXT
);

insert into notes_word(word,detail,created,phonetic) select word,detail,created,phonetic from notes_new_new;


27、加密相关

http://blog.csdn.net/androidsecurity/article/details/8666954
http://blog.csdn.net/baolong47/article/details/17335227
http://blog.csdn.net/guolin_blog/article/details/11952409


28、FaceBook开源项目

http://facebook.github.io/rebound/


29、ScrollView与高度固定的GridView或者ListView嵌套时,界面不显示在最上面

a、gvGroup.setFocusable(false); // lvGroup.setFocusable(false);在设置Adapter之前让其失去焦点
b、svMain.fullScroll(ScrollView.FOCUS_UP); // 在合适的时候(如delay 10ms)让ScrollView滚动到最顶端


30、已使用内存:

Log.i("tag", where+"usedMemory: "+Debug.getNativeHeapSize()/ 1048576L);

31、http://hold-on.iteye.com/blog/1943437

32、调用系统的浏览器打开网页:区分大小写,http必须为小写

String url = "http://www.baidu.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

33、HTTP POST 乱码

List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("v", param));
postParameters.add(new BasicNameValuePair("s", Utils.MD5Encode(param,"")));
postParameters.add(new BasicNameValuePair("e", String.valueOf(encryptType)));

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, "UTF-8");
mPost.setEntity(formEntity);

34、读取联系人

/**
 * 读取本地通讯录(手机)<br>
 * 用户通讯录: 手机号码、联系人命名、备注信息、分组信息<br>
 * 耗时
 * 
 * @param context
 * @return
 */
@SuppressLint("UseSparseArrays")
public static Map<String, ContactInfo> getContracts(Context context) {
	Map<String, ContactInfo> contactMap = new HashMap<String, ContactInfo>();
	try {

		ContentResolver resolver = context.getContentResolver();
		if (null == resolver) {
			return contactMap;
		}
		// 手机:姓名和手机号
		Cursor curosr = resolver.query(Phone.CONTENT_URI, new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
		if (null != curosr && curosr.moveToFirst()) {
			int keyId = curosr.getColumnIndex(Phone.CONTACT_ID);
			int keyName = curosr.getColumnIndex(Phone.DISPLAY_NAME);
			int keyNumber = curosr.getColumnIndex(Phone.NUMBER);
			do {
				String id = curosr.getString(keyId);
				String name = curosr.getString(keyName);
				String number = curosr.getString(keyNumber);
				ContactInfo info = new ContactInfo();
				info.setName(name);
				if(!TextUtils.isEmpty(number)){
					number = number.replace("-", "");
					number = number.replace(" ", "");
					info.setNumber(number);
				}
				contactMap.put(id, info);
			} while (curosr.moveToNext());
		}
		curosr.close();

		// SIM卡:姓名和手机号
		curosr = resolver.query(Uri.parse("content://icc/adn"), new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
		if (null != curosr && curosr.moveToFirst()) {
			int keyId = curosr.getColumnIndex(Phone.CONTACT_ID);
			int keyName = curosr.getColumnIndex(Phone.DISPLAY_NAME);
			int keyNumber = curosr.getColumnIndex(Phone.NUMBER);
			do {
				String id = curosr.getString(keyId);
				String name = curosr.getString(keyName);
				String number = curosr.getString(keyNumber);
				ContactInfo info = new ContactInfo();
				info.setName(name);
				info.setNumber(number);
				contactMap.put(id, info);
			} while (curosr.moveToNext());
		}
		curosr.close();

		// 备注
		curosr = resolver.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.DATA1 }, null, null, null);
		if (null != curosr && curosr.moveToFirst()) {
			int keyId = curosr.getColumnIndex(Data.CONTACT_ID);
			int keyRemark = curosr.getColumnIndex(Data.DATA1);
			do {
				String id = curosr.getString(keyId);
				String remark = curosr.getString(keyRemark);
				ContactInfo info = contactMap.get(id);
				if (null != info) {
					info.setRemark(remark);
				}
			} while (curosr.moveToNext());
		}
		curosr.close();

		// 获取所有分组
		Map<String, String> groupData = new HashMap<String, String>();
		curosr = resolver.query(Groups.CONTENT_URI, new String[] { Groups._ID, Groups.TITLE }, null, null, null);
		if (null != curosr && curosr.moveToFirst()) {
			int keyId = curosr.getColumnIndex(Groups._ID);
			int keyGroupName = curosr.getColumnIndex(Groups.TITLE);
			do {
				String id = curosr.getString(keyId);
				String name = curosr.getString(keyGroupName);
				groupData.put(id, name);
			} while (curosr.moveToNext());
		}
		curosr.close();

		// 关联联系人和分组
		curosr = resolver.query(Data.CONTENT_URI, new String[] { GroupMembership.CONTACT_ID, GroupMembership.GROUP_ROW_ID }, Data.MIMETYPE + " = ?",
				new String[] { GroupMembership.CONTENT_ITEM_TYPE }, null);
		if (null != curosr && curosr.moveToFirst()) {
			int keyId = curosr.getColumnIndex(GroupMembership.CONTACT_ID);
			int keyGroupId = curosr.getColumnIndex(GroupMembership.GROUP_ROW_ID);
			do {
				String id = curosr.getString(keyId);
				String groupId = curosr.getString(keyGroupId);
				String groupName = groupData.get(groupId);
				ContactInfo info = contactMap.get(id);
				if (null != info && !TextUtils.isEmpty(groupName)) {
					info.setGroup(groupName);
				}
			} while (curosr.moveToNext());
		}
		curosr.close();

		Iterator<String> iterator = contactMap.keySet().iterator();
		while (iterator.hasNext()) {
			String id = iterator.next();
			ContactInfo info = contactMap.get(id);
			System.out.println(String.format("%s, %s, %s, %s.", info.getName(), info.getNumber(), info.getGroup(), info.getRemark()));
		}

		groupData.clear();
		groupData = null;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return contactMap;
}

35、单元测试——测试异步任务

public void testAsynchronous() {
	try {
		CountDownLatch signal = new CountDownLatch(1);
		new Thread(new Runnable() {

			@Override
			public void run() {
				Looper.prepare();
				signal.countDown();
				Looper.loop();
			}
		}).start();
		signal.await();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

36、GridLayout合并

<GridLayout
    android:id="@+id/glNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etPassword"
    android:layout_centerHorizontal="true"
    android:alignmentMode="alignBounds"
    android:columnCount="3"
    android:orientation="horizontal"
    android:rowCount="4" >

    <com.ttdevs.blur.CircleButton
        android:id="@+id/cbt1"
        android:layout_width="64dp"
        android:layout_height="64dp" />

    <com.ttdevs.blur.CircleButton
        android:id="@+id/cbt2"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_margin="10dp" />

    <com.ttdevs.blur.CircleButton
        android:id="@+id/cbt3"
        android:layout_width="64dp"
        android:layout_height="64dp" />

    <com.ttdevs.blur.CircleButton
        android:id="@+id/cbt0"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_columnSpan="2"
        android:layout_gravity="right" />

    <com.ttdevs.blur.CircleButton
        android:id="@+id/cbtDel"
        android:layout_width="64dp"
        android:layout_height="64dp" />
</GridLayout>

37、PopupWindow显示的时候左右不靠边(有边缝)

setBackgroundDrawable(new ColorDrawable(0xa0000000));


38、EditText限制可输入小数

etInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

from:http://stackoverflow.com/questions/4276068/how-to-make-edittext-field-for-decimals


39、隐藏键盘

隐藏键盘貌似在onDestory中调用无效,最后自己把它写在了onStop中

// 关闭键盘
public static void close(Context context, IBinder windowToken, int flag) {
	InputMethodManager imm = (InputMethodManager) context
			.getSystemService(Context.INPUT_METHOD_SERVICE);
	imm.hideSoftInputFromWindow(windowToken, flag);
}

40、Android TextView 显示不全的问题

代码如下,当给TextView的属性设置成match_parent的时候,text中设置的文字比较长的时候会出现显示不全的问题

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:orientation="horizontal"
            android:padding="@dimen/global_padding_normal">

            <ImageView
                android:id="@+id/iv_health_light"
                android:layout_width="@dimen/detail_light_width"
                android:layout_height="@dimen/detail_light_height"
                android:gravity="center_horizontal" />

            <TextView
                android:id="@+id/tv_appraise"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/global_margin_normal"
                android:gravity="left|top"
                android:lineSpacingExtra="@dimen/global_line_space"
                android:textColor="@color/global_gray_dark"
                android:textSize="@dimen/global_font_small" />

        </LinearLayout>

41、查看系统alarm(闹钟)队列

Alarm事件队列: adb shell dumpsys alarm

查看intent: adb shell dumpsys activity intents

参考:https://plus.google.com/+RomanNurik/posts/5w3Mi9EoniT


42、singleTask与onNewIntent

singleTask与onNewIntent同时存在时,onNewIntent不是总被调用。比如:当前App启动过,第三方App再次启动时是不会调用onNewIntent的,再或者我们从任务列表中再次启动当前App也是不会触发onNewIntent的。


43、获取view的高度


Rect rect = new Rect();
getDrawingRect(rect);
int height = rect.bottom - rect.top;


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值