Archive for the ‘Android’ Category

Android真机调试访问本地服务器(localhost)的解决方案

Android系统把它自己作为了localhost!当连接localhost都是他自己啊。。 囧,在这里晕了好久才发现。。 网上介绍的都是模拟器连接本地服务器的,我试着把链接改为http://10.0.2.2/依然不可以。。 我是真机调试,不是模拟器,那怎么办呢? 解决方法 我的环境是用手机通过WIFI上网,和本地电脑在同一个局域网内。找出本地电脑的ip即可,手机可以直接访问这个IP。 如果不是在局域网内,只能把网页放到可以给外部访问的地方了(例如服务器)


Android Handler和HandlerThread使用方法

Handler的官方注释如下: A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. Handler会关联一个单独的线程和消息队列。Handler默认关联主线程,虽然要提供Runnable参数 ,但默认是直接调用Runnable中的run()方法。也就是默认下会在主线程执行,如果在这里面的操作会有阻塞,界面也会卡住。如果要在其他线程执行,可以使用HandlerThread。 Handler使用方法: Handler handler = new Handler() {   @Override public void handleMessage(Message msg) { // 处理发送过来的消息 Bundle b = msg.getData(); System.out.println("msg:" + msg.arg1); System.out.println("msg:" [...]


event.getAction()&MotionEvent.ACTION_MASK的原因

看到下面代码中用了AND位运算是为了什么呢? public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: showMsg("ACTION_DOWN" + action); break; case MotionEvent.ACTION_UP: showMsg("ACTION_UP" + action); break; case MotionEvent.ACTION_POINTER_UP: showMsg("ACTION_POINTER_UP" + action); break; case MotionEvent.ACTION_POINTER_DOWN: showMsg("ACTION_POINTER_DOWN" + action); break; } return super.onTouchEvent(event); } 首先来看看这些常量的值 ACTION_MASK 0x000000ff ACTION_DOWN 0×00000000 ACTION_UP 0×00000001 ACTION_MOVE 0×00000002 ACTION_POINTER_DOWN 0×00000005 ACTION_POINTER_UP [...]


Android ViewHolder模式

这个ViewHolder到底是什么呢?我们可以在官方sample看到这段代码 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html static class ViewHolder { TextView text; ImageView icon; } 可以看到它只是一个静态类,它的作用就在于减少不必要的调用findViewById 完整的官方例子,官方例子中convertView 也是避免inflating View。 然后把对底下的控件引用存在ViewHolder里面,再在View.setTag(holder)把它放在view里,下次就可以直接取了。 效率相差多少?看这篇文章:Android开发之ListView 适配器(Adapter)优化 /* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may [...]


Activity startActivityForResult

public void startActivityForResult (Intent intent, int requestCode) 从一个Activity启动另一个activity,得到结果返回给前一个activity。 简单说 OneActivity实现onActivityResult (int requestCode, int resultCode, Intent data)方法,然后使用startActivityForResult启动另一个Activity 另一个Activity取得结果后通过setResult (…)把结果传回。 代码如下 界面代码不提供了,两个都是一个简单的button 注意要在AndroidManifest.xml定义你新增的Activity <activity android:name=".OtherActivity"></activity> 第一个Activity package com.fatkun;   import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast;   public class OneActivity extends Activity { private final int myRequestCode = 1; //请求码 [...]


Android BroadcastReceiver 广播

总结如下: 广播可用于Service与Activity的之间的通信,也可用于接收一些系统的事件,例如收到短信,电量等信息。 有两种方法注册,静态注册和动态注册 静态注册 创建一个类继承BroadcastReceiver,然后在AndroidManifest.xml 添加 <receiver android:name="clsReceiver2"> <intent-filter> <action android:name="com.testBroadcastReceiver.Internal_2"/> </intent-filter> </receiver> 动态注册 继承BroadcastReceiver类,实现onReceive方法。然后registerReceiver它。同一个Receiver还可以“听多个广播”,可以在IntentFilter加多个action。 主要通过IntentFilter,别人用sendBroadcast(intent)发广播,如果频率一样(IntentFilter里的Action一样)就可以听到广播。 //动态注册广播消息 registerReceiver(bcrIntenal1, new IntentFilter(INTENAL_ACTION_1)); //取消广播接收器 unregisterReceiver(rhelper); 两篇参考文章: http://blog.csdn.net/hellogv/article/details/5999170 http://www.cnblogs.com/jico/articles/1838293.html