监听android home键的实现方式

1.首先是创建一个广播接受者

2.注册监听

可以在Activity里注册,也可以在Service,Application里面

完整的代码如下:

参考链接:监听android home键的实现方式

java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)

在使用AsyncTask的时候,收到了如下的错误信息:

查了一下文档,才了解到AsyncTask不可以复用。The task can be executed only once (an exception will be thrown if a second execution is attempted.)
AsyncTask文档链接AsyncTask。关键信息摘抄如下:

Threading rules


There are a few threading rules that must be followed for this class to work properly:

  • The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
  • The task instance must be created on the UI thread.
  • execute(Params...) must be invoked on the UI thread.
  • Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
  • The task can be executed only once (an exception will be thrown if a second execution is attempted.)

JS获得element的index

获得某个element在parent中的index。

jquery提供了index接口,可以直接拿到。

如果没有jquery的话,

将document查询到的HTML Collection转为Array,

然后使用Array的index接口拿到index。