Android-用HttpClient抓取html页面内容
用的类库为commons-httpclient-3.1.jar.有兴趣的下载去。代码如下:
private String getHtmlContent(final String url) {
String result = "";// 返回的结果
StringBuffer resultBuffer = new StringBuffer();
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(url);
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GB2312");
getMethod.getParams().setContentCharset("GB2312");
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 流式读取
// 读取内容
// byte[] responseBody = getMethod.getResponseBody();
// 处理内容
// String result = new String(responseBody,"GBK");
// result = getMethod.getResponseBodyAsString();
// System.out.println(result);
// System.out.println(getMethod.getResponseCharSet());
// 推荐做法
BufferedReader in = new BufferedReader(new InputStreamReader(
getMethod.getResponseBodyAsStream(), getMethod
.getResponseCharSet()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
result = new String(resultBuffer);
return result;
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
return result;
}
Flex 4 List Scrolling on Android with Flash Player 10.1
Learned from http://www.jamesward.com/2010/02/19/flex-4-list-scrolling-on-android-with-flash-player-10-1/
One of the challenges of running existing web content on mobile devices is that user interactions differ between mediums. For instance, on a normal computer with a mouse, scrolling though lists is often done by clicking on scroll bars or mouse wheels. On mobile devices that lack a pointing device this is not the best interaction paradigm. On devices with touch screens the paradigm for scrolling is usually a swipe gesture.
In Flash Player 10.1 there are APIs for gestures and multitouch events. I thought it would be fun to hook up the list scrolling on a Flex 4 List to the TouchEvent on my Nexus One. Check out the video:
If you want to see how I created this simple demo, check out the source code. Let me know if you have any questions.
---------------------------------------------------------------------------------------------------------------------
视频很卡,只是随便看了下。看源代码的情况,是一个twitter的list列表展示。
看起来,Flash Player在Android下边运行的不错,希望赶紧出个可以直接发布成APK的Flash IDE吧。
不过真的要是发布了,那也是个头疼的事儿,到底是用Flash开发呢还是用Eclipse开发?
Flash的效率会是如何?对硬件的支持呢?慢慢等吧……只有尝试了才知道。
Android-MotoDev Shop4Apps
MotoDev Shop4Apps对中国开放,目前豆腐的状态是刚注册了下,具体还没有发布,晚上回去测试。
另外就是我也没下载Moto的Market客户端,里边的Apk介绍希望做的比Google Market好一点,不然,,只能说两个字,杯具。
来几张可爱的图:



很Q,很可爱~
Android-OnGestureListener接口
在做应用的时候难免会碰到触摸事件,Android提供了不少事件,只要实现OnGestureListener接口,并且呢,在onTouch方法里写个:
//记得要implements OnTouchListener,OnGestureListener
//public class mGestureDetector extends Activity implements OnTouchListener,OnGestureListener
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
View v = new View(this);
setContentView(v);
//mGestureDetector是个GestureDetector对象,记得初始化一下。
mGestureDetector = new GestureDetector(this);
//另外呢,需要针对onFling方法的监听,需要在监听的View里边加上:
v.setOnTouchListener(this);
v.setLongClickable(true);
}
public boolean onTouch(View v, MotionEvent mo) {
return mGestureDetector.onTouchEvent(mo);
}
另外是OnGestureListener接口必须实现的几个方法,具体要怎么实现大家自个儿对号入座吧。
public boolean onDown(MotionEvent e) {
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return true;
}
Android – AlertDialog,Dialog
丢一个自定义AlertDialog的例子。
AlertDialog.Builder alert = new AlertDialog.Builder(aBrainExploration.this);
alert.setTitle(R.string.label_enterOneName);
// Set an EditText view to get user input
final EditText input = new EditText(aBrainExploration.this);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Appendable value = input.getText();
// setTitle(value.toString());
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});
alert.show();
相对来说有个更轻量级的Dialog类,用法也是差不多~
Android-ImageView setAlpha()的问题
不知道大家有没碰到这种情况,使用同一个图片资源做背景的ImageView,使用setAlpha之后,全部使用该图片资源的ImageView都会被影响到,导致这个问题的原因和解决方案如下:http://android-developers.blogspot.com/2009/05/drawable-mutations.html (PS:被和谐,需要爬墙。)
来个例子说明下,我们需要生成10个ImageView,偶数位的ImageView设置alpha,代码如下:
private int res_ico = R.drawable.icon;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout l= (LinearLayout)findViewById(R.id.layout1);
for(int i=0;i < 10;i++){
ImageView iv = new ImageView(this);
iv.setImageResource(res_ico);
iv.setClickable(false);
iv.setAdjustViewBounds(true);
if(i%2==0){
ico.setAlpha(125);
}
l.addView(iv);
}
}
这样的话就会发现,所有的ImageView的透明度是和最后设置的值一样的,具体原因在上边的网页里有说明,因为大家用的都是一个资源嘛。
那么怎么解决这个问题呢?
方案如下:
private int res_ico = R.drawable.icon;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout l= (LinearLayout)findViewById(R.id.layout1);
for(int i=0;i < 10;i++){
Drawable ico = getResources().getDrawable(res_ico);
ImageView iv = new ImageView(this);
iv.setBackgroundDrawable(ico);
iv.setClickable(false);
iv.setAdjustViewBounds(true);
if(i%2==0){
ico.mutate().setAlpha(125);
}
l.addView(iv);
}
}
mutate()方法是让图片资源mutable,内部工作原理应该就是克隆了一份自己。
另外:
Drawable ico = getResources().getDrawable(res_ico);
要放在循环里,放在循环体外的话照样会出问题,囧。
Android-G3的OOXX
1.关于重启
同时按 通话键+menu+挂断键。
类似于电脑的Ctrl+Alt+Del,还蛮搞的一个设置。
2.关于Gmail帐号的更换。
进入设置-》应用程序-》管理应用程序。清除掉Gmail,Gmail 存储里的数据,然后重启手机,就可以重新设置了,不需要恢复出厂设置。
如果您老用的是英文系统,自己对照翻译吧~
3.关机
按挂机键点亮屏幕后再长按挂机键同时按MENU键会出关机选择菜单。
Android-一些常用技巧
1.hideStatusbarAndTitlebar()隐藏statusbar和titlebar.
private void hideStatusbarAndTitlebar() {
final Window win = getWindow();
// No Statusbar
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
2.设置屏幕显示模式ScreenOrientation.
在activity里设置android:screenOrientation的值。
android:screenOrientation的属性有以下值:
unspecified(默认值,由系统判断状态自动切换),The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.
landscape,横屏
portrait,竖屏
user(用户当前设置的orientation值),The user's current preferred orientation.
behind(下一个要显示的Activity的orientation值),The same orientation as the activity that's immediately beneath it in the activity stack.
sensor(传感器的方向),The orientation determined by a physical orientation sensor. The orientation of the display depends on how the user is holding the device; it changes when the user rotates the device.
nosensor(不使用传感器,这个效果差不多等于unspecified).An orientation determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.
3.水平/垂直居中的方法.
设置parent的android:gravity为"center"。
4.获得当前屏幕宽高的方法.
Display display = getWindowManager().getDefaultDisplay(); Config.screenWidth = display.getWidth(); Config.screenHeight = display.getHeight();
APK-aPKMyFriends(名字大作战)
先来介绍下aPKMyFriends,aPKMyFriends到底是个什么东西呢?
先来看一个以前做的Flash game:NamePK
啊,不知道当时为什么做的是英文版的UI……这个游戏很简单,输入一个名字,再输入另一个,2个名字就开始PK,最后有一个会变扁趴下,举个例子,输入豆腐和豆奶,可能会得到这样的结果:
------------------------------------------------------------------------------------------------
比对敏捷值 豆奶 获得优先攻击权
豆奶 高唱我的菊花为谁开 伤害值为 117
但是 豆腐 闪开了
豆腐 搬起一块石头砸了过来 伤害值为 87
但是 豆奶 闪开了
豆奶 喝下一口酒精喷出一个大火球 伤害值为 109
但是 豆腐 闪开了
豆腐 使出了砖石星辰拳 伤害值为 96
豆奶 搬起一块石头砸了过来 伤害值为 126
但是 豆腐 闪开了
豆腐 搬起一块石头砸了过来 伤害值为 89
但是 豆奶 闪开了
豆奶 扔出了发着佛光的雅典娜 伤害值为 121
豆腐 喝下一口酒精喷出一个大火球 伤害值为 87
豆奶 喝下一口酒精喷出一个大火球 伤害值为 128
豆腐 喝下一口酒精喷出一个大火球 伤害值为 81
豆奶 扔出了发着佛光的雅典娜 伤害值为 121
豆腐 扔出了发着佛光的雅典娜 伤害值为 86
但是 豆奶 闪开了
豆奶 喝下一口酒精喷出一个大火球 伤害值为 136
豆腐 被扁趴下 豆奶 获得胜利
------------------------------------------------------------------------------------------------
Android-各种Layout对象之FrameLayout,RelativeLayout
FrameLayout也是一个不常用到的布局容器:
FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).
FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。FrameLayout的大小是最大的子元素的大小。
----------------------------------------------------------------------------------------------
RelativeLayout是用的比较多的一个Layout:
A Layout where the positions of the children can be described in relation to each other or to the parent. For the sake of efficiency, the relations between views are evaluated in one pass, so if view Y is dependent on the position of view X, make sure the view X comes first in the layout.
看描述是一个相对来说很自由的容器,比较麻烦的是每个需要被用来定位的容器都需要指定一个id。
下边直接来一个例子吧,比较好理解:
< ?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<textview android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello man"/>
<edittext android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label"/>
<button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:text="OK"
/>
<button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/ok"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="Cancel" />
</relativelayout>
效果:

RelativeLayout的参数相比其他Layout是非常多……而且在实际应用中应该是各种Layout相互嵌套……
唉,为何要有那么多的分辨率?为何?为虾米??!
Android-各种Layout对象之TableLayout,AbsoluteLayout
TableLayout,顾名思义,就是一个表格布局的容器:
TableLayout positions its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells. Each row has zero or more cells; each cell can hold one View object. The table has as many columns as the row with the most cells. A table can leave cells empty. Cells cannot span columns, as they can in HTML.
TableLayout将子元素的位置分配到行或列中。一个TableLayout由许多的TableRow组成,每个TableRow都会定义一个row(事实上,你可以定义其它的子对象,这在下面会解释到)。TableLayout容器不会显示row、cloumns 或cell的边框线。每个row拥有0个或多个的cell;每个cell拥有一个View对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与HTML中的不一样。
Columns can be hidden, can be marked to stretch to fill available screen space, or can be marked as shrinkable to force the column to shrink until the table fits the screen. See the reference documentation for this class for more details.
列可以被隐藏,也可以被设置为伸展的从而填充可利用的屏幕空间,也可以被设置为强制列收缩直到表格匹配屏幕大小。对于更详细信息,可以查看 TableLayout类的参考文档。 (PS:如果发现该页被和谐,请蛋定,蛋定~)
--------------------------------------分割线----------------------------------------
下边是基本上不太用到的AbsoluteLayout,就是绝对定位,这个跟HTML里的差不多:
AbsoluteLayout enables children to specify exact x/y coordinates to display on the screen, where (0,0) is the upper left corner, and values increase as you move down or to the right. Margins are not supported, and overlapping elements are allowed (although not recommended). We generally recommend against using AbsoluteLayout unless you have good reasons to use it, because it is fairly rigid and does not work well with different device displays.
AbsoluteLayout可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用 AbsoluteLayout,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。
官方都不推荐用了,你觉得你在什么时候会用到?有时候的浮动广告?似乎是个不错的注意。