2014年7月16日星期三

get the URI of an image resource in android

Reference:http://androidbook.blogspot.jp/2009/08/referring-to-android-resources-using.html

Two methods:

  1. android.resource://[package]/[res id]
  2. android.resource://[package]/[res type]/[res name]

Example:

  1. Uri.parse("android.resource://com.example.test/“ + R.drawable.ic_launcher)
  2. Uri.parse("android.resource://com.example.test/drawable/ic_launcher")

2014年7月15日星期二

修改长按home后出现的app

长按home默认出现的是Google Search,可以改成选择任意的Activity:

   <intent-filter>
      <action android:name="android.intent.action.ASSIST" />
      <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>

     <meta-data
        android:name="com.android.systemui.action_assist_icon"
        android:resource="@android:drawable/sym_def_app_icon" />

2014年6月27日星期五

Android sdk manger cannot download anything in China

因为goagent不能用了,所以只能用改hosts了。
1.先选中Force https://... sources to be fetched using http://...
2. hosts里面添加:
203.208.46.146 dl.google.com
203.208.46.146 dl-ssl.google.com

2014年6月15日星期日

proguard与gson,searchview (v7)

与proguard同时使用要注意:

使用gson时(参见https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg):
##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------

使用android support v7中的searchview时:
-keep class android.support.v7.widget.SearchView { *; }

2013年9月2日星期一

判断statusbar是否存在

private boolean checkStatusbar(){
   Rect rect=new Rect();
   getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
   //屏幕高度还有别的办法得到,不赘述
   return (rect.bottom-rect.top)
           ==getResources().getDisplayMetrics().heightPixels;
}

另一种计算一般statusbar高度的方法(固定值,不是实时的高度):
(参考http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/)
private int getStatusBarHeight() {
   int result = 0;
   int resourceId = getResources()
           .getIdentifier("status_bar_height", "dimen", "android");
   if (resourceId > 0) {
         result = getResources().getDimensionPixelSize(resourceId);
   }
 return result;
}

2013年8月31日星期六

onTouchEvent, onClick及onLongClick中各事件的顺序

(首先要知道:Returns true if the listener consumed event, false otherwise.)

onTouchEvent的一般过程:
   down-- move --up

全返回false:
down--move--longClick--move--up--onClick

onLongClick返回true:
down--move--longClick--move--up

up返回true:
短按:
down--move--up------longClick(??why?)
长按:
down--move--longClick--move--up

move返回true:
无影响

down返回true:  (不是点击事件)
短按:
没有click
长按:
没有click和longClick