首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(23)

Step 39. InputConsumer.sendFinishedSignal 这个函数定义在frameworks/base/libs/ui/InputTransport.cpp文件中: status_tInputConsumer::sendFinishedSignal(){ ...... returnmChannel->sendSignal(INPUT_SIGNAL_FINISHED); } 这个函数的实现很简单,只是调用其内部对象mChannel的sendSignal函数来执行发送信号的通知。前面我们已经说过,这里的mChannel的类型为InputChannel,它是注册在应用程序一侧的Client端InputChannel,它的成员函数sendSignal的定义我们在上面的Step 20中已经分析过了,这里不再详述,不过,这里和上面Step 20不一样的地方是,它里的通知方向是从反向管道的写端(在应用程序这一侧)到反向管道的读端(在InputDispatcher这一侧)。 前面我们在分析应用程序注册键盘消息接收通道的过程时,在Step 18(InputDispatcher.registerInputChannel)中,说到InputDispatcher把一个反向管道的读端文件描述符添加到WindowManagerService所运行的线程中的Looper对象中去,然后就会在这个反向管道的读端上睡眠等待有这个管道有新的内容可读。现在,InputConsumer往这个反向管道写入新的内容了,于是,InputDispatcher就被唤醒过来了,唤醒过来后,它所调用的函数是InputDispatcher.handleReceiveCallback函数,这与前面的Step 21的逻辑是一样的。 Step 40. InputDispatcher.handleReceiveCallack 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: intInputDispatcher::handleReceiveCallback(intreceiveFd,intevents,void*data){ InputDispatcher*d=static_cast<InputDispatcher*>(data); {//acquirelock AutoMutex_l(d->mLock); ssize_tconnectionIndex=d->mConnectionsByReceiveFd.indexOfKey(receiveFd); ...... nsecs_tcurrentTime=now(); sp<Connection>connection=d->mConnectionsByReceiveFd.valueAt(connectionIndex); ...... status_tstatus=connection->inputPublisher.receiveFinishedSignal(); if(status){ ...... return0;//removethecallback } d->finishDispatchCycleLocked(currentTime,connection); ...... return1; }//releaselock } 这个函数首先是通过传进来的receiveFd参数(反向管道的读端文件描述符)的值取得相应的Connection对象: ssize_tconnectionIndex=d->mConnectionsByReceiveFd.indexOfKey(receiveFd); ...... sp<Connection>connection=d->mConnectionsByReceiveFd.valueAt(connectionIndex); 然后通过调用这个connection对象的内部对象inputPublisher的receiveFinishedSignal函数来确认是否真的收到键盘事件处理完成的信号,确认之后,就会调用InputDispatcher对象d的finishDispatchCycleLocked函数来执行一些善后工作。下面我们就依次分析这两个过程。 Step 41. InputPublisher.receiverFinishedSignal 这个函数定义在frameworks/base/libs/ui/InputTransport.cpp文件中: status_tInputPublisher::receiveFinishedSignal(){ .... charsignal; status_tresult=mChannel->receiveSignal(&signal); if(result){ returnresult; } if(signal!=INPUT_SIGNAL_FINISHED){ ....... returnUNKNOWN_ERROR; } returnOK; } 这里的逻辑和前面的Step 22中NativeInputQueue确认是否真的收到键盘事件分发的信号的逻辑是一致的,都是通过InputChannel的receiveSignal函数来确认是否在管道中收到了某一个约定的字符值,不过,这里约定的字符值为INPUT_SIGNAL_FINISHED。 回到前面的Step 40中,确认了是真的收到了键盘事件处理完成的信号后,就调用InputDispatcher的finishDispatchCycleLocked函数来执行一些善后工作了。 Step 42. InputDispatcher.finishDispatchCycleLocked 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: voidInputDispatcher::finishDispatchCycleLocked(nsecs_tcurrentTime, constsp<Connection>&connection){ ...... //Notifyothersystemcomponents. onDispatchCycleFinishedLocked(currentTime,connection); //Resetthepublishersincetheeventhasbeenconsumed. //Wedothisnowsothatthepublishercanreleasesomeofitsinternalresources //whilewaitingforthenextdispatchcycletobegin. status_tstatus=connection->inputPublisher.reset(); ...... startNextDispatchCycleLocked(currentTime,connection); } 这个函数主要就是做了三件事情: 一是通知其它系统,InputDispatcher完成了一次键盘事件的处理: //Notifyothersystemcomponents. onDispatchCycleFinishedLocked(currentTime,connection); 二是调用相应的connection对象的内部对象inputPublisher来的reset函数来回收一些资源,它里面其实就是释放前面在Step 18(InputPublisher.publishKeyEvent)使用的匿名共享内存了: //Resetthepublishersincetheeventhasbeenconsumed. //Wedothisnowsothatthepublishercanreleasesomeofitsinternalresources //whilewaitingforthenextdispatchcycletobegin. status_tstatus=connection->inputPublisher.reset(); 三是调用InputDispatcher的startNextDispatchCycleLocked函数来处理下一个键盘事件: startNextDispatchCycleLocked(currentTime,connection); 因为正在处理当前这个键盘事件的时候,很有可能又同时发生了其它的键盘事件,因此,这里InputDispatcher还不能停下来,需要继续调用startNextDispatchCycleLocked继续处理键盘事件,不过下一个键盘事件的处理过程和我们现在分析的过程就是一样的了。 至此,InputManager分发键盘消息给应用程序的过程就分析完成了,这是一个比较复杂的过程,不过,只要我们抓住主要的线索,就不难理解了,现在我们就小结一下这个过程的四个主要线索: A. 键盘事件发生,InputManager中的InputReader被唤醒,此前InputReader睡眠在/dev/input/event0这个设备文件上; B.InputReader被唤醒后,它接着唤醒InputManager中的InputDispatcher,此前InputDispatcher睡眠在InputManager所运行的线程中的Looper对象里面的管道的读端上; C. InputDispatcher被唤醒后,它接着唤醒应用程序的主线程来处理这个键盘事件,此前应用程序的主线程睡眠在Client端InputChannel中的前向管道的读端上; D. 应用程序处理处理键盘事件之后,它接着唤醒InputDispatcher来执行善后工作,此前InputDispatcher睡眠在Server端InputChannel的反向管道的读端上,注意这里与第二个线索处的区别。 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966648,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android实现仿IOS带清空功能的文本输入框

/** *@类名:ClearableEditText *@功能描述: *@作者:WilliamXu *@创建日期:2013-4-13 *@修改人: *@修改日期: *@修改备注: *@版本号:1.0 */ publicclassClearableEditTextextendsEditTextimplements OnFocusChangeListener,TextWatcher{ privateDrawablexD; publicClearableEditText(Contextcontext){ super(context); init(); } publicClearableEditText(Contextcontext,AttributeSetattrs){ super(context,attrs); init(); } publicClearableEditText(Contextcontext,AttributeSetattrs,intdefStyle){ super(context,attrs,defStyle); init(); } privatevoidinit(){ xD=getCompoundDrawables()[2]; if(xD==null){ xD=getResources() .getDrawable(R.drawable.search_clear); } xD.setBounds(0,0,xD.getIntrinsicWidth(),xD.getIntrinsicHeight()); setClearIconVisible(false); super.setOnFocusChangeListener(this); addTextChangedListener(this); } @Override publicvoidsetOnFocusChangeListener(OnFocusChangeListenerf){ this.f=f; } privateOnFocusChangeListenerf; @Override publicbooleanonTouchEvent(MotionEventevent){ if(getCompoundDrawables()[2]!=null){ if(event.getAction()==MotionEvent.ACTION_UP){ booleantappedX=event.getX()>(getWidth() -getPaddingRight()-xD.getIntrinsicWidth()); if(tappedX){ setText(""); event.setAction(MotionEvent.ACTION_CANCEL); } } } returnsuper.onTouchEvent(event); } @Override publicvoidonFocusChange(Viewv,booleanhasFocus){ if(hasFocus){ setClearIconVisible(getText().length()>0); }else{ setClearIconVisible(false); } if(f!=null){ f.onFocusChange(v,hasFocus); } } protectedvoidsetClearIconVisible(booleanvisible){ Drawablex=visible?xD:null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],x,getCompoundDrawables()[3]); } @Override publicvoidonTextChanged(CharSequences,intstart,intcount, intafter){ setClearIconVisible(s.length()>0); } @Override publicvoidbeforeTextChanged(CharSequences,intstart,intcount, intafter){ //TODOAuto-generatedmethodstub } @Override publicvoidafterTextChanged(Editables){ //TODOAuto-generatedmethodstub } } 本文转自william_xu 51CTO博客,原文链接:http://blog.51cto.com/williamx/1195051,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android Ap 开发 设计模式第九篇:桥接模式

Bridge Pattern 模式解读 bridge 是“桥梁”的意思。就像现实社会里的桥梁担负着连接两岸的重大责任一样,Bridge Pattern也是负责连接2个不同位置的参与者。 桥接模式沟通着“功能的类层次”和“实现的类层次”。将“功能的类层次”和“实现的类层次”分成2个独立的类层次可能会弄得支离破碎,所以必须在2个类层次之间建一座沟通的桥梁。 程序示例 按照上面的分法,本例可以分为4个类按照“功能的类层次”和“实现的类层次”分开,分别为: 功能的类层次 Display 该类为打印内容的类 CountDisplay 该类不仅可以打印内容还可以参数指定打印次数 实现的类层次 DisplayImpl 该类为抽象类,抽象出打印内容所需要的功能 StringDisplayImpl 该类实现了DisplayImpl 所抽象出来的功能 按照如上划分,制作出了UML图,如下: 功能的类层次:Display 类,代码: public class Display{ private DisplayImplimpl; public Display(DisplayImplimpl){ this .impl = impl; } public Stringopen(){ return impl.rawOpen(); } public Stringprint(){ return impl.rawPrint(); } public Stringclose(){ return impl.rawClose(); } public finalStringdisplay(){ open(); Stringtext = print(); close(); return text; } } 该类通过参数将DisplayImpl传进来,直接让Display 有DisplayImpl的功能。该功能也是Builder Pattern 的模式。位于“功能的类层次”最上层的类,利用接口参与者的方法只记载基本功能的类。这个对象实例是保持住接口参与者。该类为扮演这个参与者的类。 功能的类层次:CountDisplay类 ,代码: public class CountDisplayextendsDisplay{ public CountDisplay(DisplayImplimpl){ super(impl); // TODOAuto-generatedconstructorstub } public StringmultiDisplay( int times){ StringBuffersb = new StringBuffer(); sb.append(open() + " \n " ); for ( int i = 0 ;i < times;i ++ ){ sb.append(print() + " \n " ); } return sb.append(close()).toString(); } } 该类系Display 的子类,并且再添加一个可按照次数循环打印的方法。对抽象参与者新增功能的参与者(改良后的抽象化),该类扮演在这个DEMO中扮演着这个角色。 实现的类层次:DisplayImpl 类,代码: public abstract class DisplayImpl{ public abstract StringrawOpen(); public abstract StringrawPrint(); public abstract StringrawClose(); } 该类位于实现的类层次的最上层,分别抽象了rawOpen、rawPrint、rawClose三个方法,这三个方法分别对应到Display 类的open、print、close方法,执行前处理、打印和后处理。规定要实现的抽象参与者的接口 。 实现的类层次:StringDisplayImpl 类,代码: public class StringDisplayImplextendsDisplayImpl{ private String string ; public StringDisplayImpl(Stringstr){ this . string = str; } @Override public StringrawClose(){ // TODOAuto-generatedmethodstub return printLine(); } @Override public StringrawOpen(){ // TODOAuto-generatedmethodstub return printLine(); } @Override public StringrawPrint(){ // TODOAuto-generatedmethodstub return " | " + string + " | " ; } private StringprintLine(){ StringBuffersb = new StringBuffer(); sb.append( " + " ); for ( int i = 0 ;i < string .length();i ++ ){ sb.append( " - " ); } return sb.append( " + " ).toString(); } } 该类实现了DisplayImpl的三个抽象方法,本例中唯一一处实现功能的也是由本类完成。具体实现接口 参与者的接口 。 界面入口:BridgePatternActivity 类,代码: public class BridgePatternActivityextendsActivity{ /* *Calledwhentheactivityisfirstcreated. */ @Override public void onCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); ((Button)findViewById(R.id.Button01)).setOnClickListener( new OnClickListener(){ @Override public void onClick(Viewv){ // TODOAuto-generatedmethodstub Displaydisplay = new Display( new StringDisplayImpl( " terry_龙 " )); CountDisplaymCountDisplay = new CountDisplay( new StringDisplayImpl( " terry " )); ((EditText)findViewById(R.id.EditText01)).setText(display.print()); ((EditText)findViewById(R.id.EditText02)).setText(mCountDisplay.multiDisplay( 5 )); } }); } } 运行结果: 代码下载: 桥接模式 本文转自 terry_龙 51CTO博客,原文链接:http://blog.51cto.com/terryblog/624082,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码