7月 112015
 

1. 在AndroidManifest.xml中加入以下权限

2. 注册网络变化监听事件,在AndroidManifest.xml中加入以下代码

android:name的值为NetworkChangeReceiver为类名

3,实现NetworkChangerReceiver类

 

11月 012014
 

  ContextMenu相当于windows中的右键弹出菜单,在android中一般要长按某个view才会激活这个弹出菜单,但是有些情况下我们想单击就可以打开这个菜单,以提高用户的使用体验。那么如何才能单击就能打开这个菜单呢,其实很简单,我们在view的onClick事件中调用openContextMenu()即可打开这个弹出菜单了。下面是一个代码片断:

  imgcapture是一个ImageView,此代码执行后,单击imgcapture即可触发onCreateContextMenu事件,然后弹出菜单。另外,必须要调用registerForContextMenu注册这个菜单,否则单击和长按都无法弹出这个菜单。

  如果要弹出比较复杂的菜单的话就用使用Dialog来实现了。

MotionEvent详解

 分类:Android, Java 阅读 (2,679)  No Responses »
10月 092014
 

MotionEvent用来向界面发送鼠标\手写笔\手指或者轨迹球的事件,一般包括硬件的动作类型及一些坐标轴信息。

getPointerCount()
有几个点同时触发了此事件,用于多点触控。

final float getPressure()
第一个触点的压力值(也有可能是任意某个触点)
final float getPressure(int pointerIndex)
返回指定的pointerIndex触点的当前压力值(使用getPointerId(int)来获取此触点的pointerIndex值)

final float getRawX()
Returns the original raw X coordinate of this event.

final float getRawY()
Returns the original raw Y coordinate of this event.

final float getSize(int pointerIndex)
得到触点触摸屏幕的面积值,此值的意义是个相对值,具体计算方法不清楚。(使用getPointerId(int)来获取此触点的pointerIndex值).
final float getSize()
getSize(int) for the first pointer index (may be an arbitrary pointer identifier).

getDownTime()
返回当屏幕刚被按下时的时间(毫秒),按下后移动此时间不变。

final long getEventTime()
返回MotionEvent所在的事件被激发的时间(毫秒),如在OnTouchListener(MotionEvent event)中,每个OnTouch被激发的时间。

4月 172013
 

 

其他注意事项:

1、如何设置自己的Dialogfragment没有标题栏?
可以通过两种方法来设置 ,一种是使用dialogfragment的setStyle函数,另外就是使用getDialog().getWindow().requestFeature方法,具体代码如下

使用getDialog的方法

2. The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentManager, String)

此问题常是由于你所自定义的DialogFragment子类继承自android.app.dialogfragment,而非继承自android.support.v4.app.DialogFragment,解决办法就是将你的自定义的DialogFragment子类继承自android.support.v4.app.DialogFragment。如下所示:

 

1月 212013
 

  我们有时会在开发中,使用按钮的focus选项来实现类似Tab标签页的效果,当用户按下某个按钮后,这个按钮就显示另外一种被按下的效果,以表示用户当前浏览的是哪种类型数据。允许组件有焦点效果首先要设置focusable属性为true,然后把android:focusableInTouchMode属性也设置为true。

  需要注意的是,当设置组件可以获得焦点的时候,这个组件就不能响应android:onClick事件了,如果你把实现功能的代码写到onClick事件里的话,是不能看到程序执行的。那么这时候我们就得用组件的setOnFocusChangeListener来设置一个事件来监听组件获得和失去焦点的状态,然后把相关的代码放到这个事件里。下面是一个代码片断:

 

  其中hasFocus为true时为组件获得焦点,被按下的状态,为false时为其他组件获得焦点此组件失去焦点时触发。

在Android中使用TabHost

 分类:Android 阅读 (1,698)  No Responses »
1月 032013
 

使用TabHost的注意事项:

  1. 在API13 Android3.2之后Android的SDK中提供了ActionBar,可以做出TabHost的效果,而且还有很多其他的功能,所以就不推荐使用TabHost了
  2. 使用TabHost的类必须继承自TabActivity,然后通过getTabHost()来获取界面上的TabHost组件
  3. TabHost在Layout.xml中,id属性必须定义为如下格式:android:id=”@android:id/tabhost”

 

8月 202012
 

* 本例使用ListView和自定义的行部局实现了一个最基本的表格部局
* 和一个可变长度的表格部局
* 并实现的简单的行组件的单击事件

XML文件1 activity_list_view_table.xml

XML文件2 listview_row_basic.xml

XML文件3 listview_row.xml

JAVA代码文件 ListViewTableActivity.java

———————————

点击此下下载源代码

———————————

8月 132012
 

在Android中连接无线网络,输入密码后,“连接”按钮为灰色,不能使用。

原因:

1、你使用的无线路由器的密码小于8位,android系统中当无线密码不足8位时候,连接按钮不能使用,解决办法:更改路由器的密码为大于等于8位的。

8月 092012
 

本例主要是使用了Intent对象的隐式调用方式,设置intent的Action, Data和Extra来传递的相关的信息

activity布局代码如下:

类代码如下:

本代码调试环境:

* Eclipse: Indigo Service Release 2
* Android SDK: 4.0(API LEVEL 14)

点击这里下载CallMailApp

注意:

1. 如果你还没有在你的手机或者AVD上创建相关的email帐户,运行此程序时会提示Unsupported action(不支持的Action)!

8月 082012
 

1、为什么要使用service?

在我们开发的应用中,有的时候需要做一些长期运行的任务,比如下载文件、音乐播放器、闹钟或者邮件程序等。由于Android系统会根据系统资源使用情况自动清理闲置的后台程序,所以普通的Activity程序在运行一段时间后就有可能被系统清理掉,不能达到长期运行的效果。而包含service的进程属于优先权比较高的(关于进程的生命周期相关知识请点击这里查看)不容易被系统清理掉,所以对那些应用来说,选择service是必要的。

2、创建一个Service

在工程节点或者包节点上右键,选择”New->Class”,输入包名,类名称,SuperClass输入或者选择android.app.Service,点击ok,创建一个服务类成功,创建完成后初始化代码如下:

创建完成后还无法使用此服务,需要在AndroidManifestXml中声明该服务类,示例如下:

3、服务类的一些重要方法

getApplication()

Return the application that owns this service.
onBind(Intent intent)

Return the communication channel to the service.
onConfigurationChanged(Configuration newConfig)

Called by the system when the device configuration changes while your component is running.
onCreate()

Called by the system when the service is first created.
onDestroy()

Called by the system to notify a Service that it is no longer used and is being removed.
onLowMemory()

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
onRebind(Intent intent)

Called when new clients have connected to the service, after it had previously been notified that all had disconnected in itsonUnbind(Intent).
onStart(Intent intent, int startId)

This method was deprecated in API level 5. Implement onStartCommand(Intent, int, int) instead.
onStartCommand(Intent intent, int flags, int startId)

Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
onTaskRemoved(Intent rootIntent)

This is called if the service is currently running and the user has removed a task that comes from the service’s application.
onTrimMemory(int level)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.
onUnbind(Intent intent)

Called when all clients have disconnected from a particular interface published by the service.
startForeground(int id, Notification notification)

Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state.
stopForeground(boolean removeNotification)

Remove this service from foreground state, allowing it to be killed if more memory is needed.
stopSelf()

Stop the service, if it was previously started.
stopSelf(int startId)

Old version of stopSelfResult(int) that doesn’t return a result.
stopSelfResult(int startId)

Stop the service if the most recent time it was started was startId.

4、Service的调用方法

Service可能通过两种方法启动,一种是通过startService另外一种是通过bindService

1) startServie的声明如下:public abstract ComponentName startService (Intent service)

参数说明:service是你要启动的服务对象,通过Intent构造,你可以在Intent对象中加入其他需要的参数,以在服务中使用。执行此函数后,如果服务未启动则系统会创建此服务并执行,如果执行此函数时服务已经在运行,则服务会继续执行。

每次调用startService时都会触发服务内的android.app.Service.onStartCommand方法,此机制提供一个方便的方法通过Intent参数传入一些需要的参数,以避免必须再使用BindService才能实现的功能。

终止由startService启动的服务方法:一种是可以在服务内部调用stopSelf()来停止服务,另一种是在调用startService的context中通过调用stopService来终止

2) bindService声明如下:public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)

参数说明:
service: 是要绑定的服务的实例标识,
conn: 用于接收成功绑定或者结束绑定时的通知,在连接时可以接收service返回的对象,
flags: 为绑定选项,可能为0, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT, BIND_WAIVE_PRIORITY.

对于由startService启动的服务,也可以使用此函数进行绑定。如果服务没有启动,可以设置flags为BIND_AUTO_CREATE,这样执行此函数时服务会被自动创建。

只有当调用该服务存在的时候系统才认为该服务有必须继续执行代码,比如当调用该服务的Activity停止或者销毁后它绑定的服务也会停止工作。所以在执行长期操作的时候不要在bindService中执行,即不要在Service的onbind函数中启动相应的定时器,否则当activity结束后,该定时器也会不再执行工作,而应该先用startService启动服务,并在服务的onStartCommand中执行定时器操作。

 

注意:

1、在使用一个service前必须在AndroidManifest.xml中定义该服务类名称,如:<service android:name=”Em8CheckerService”></service>