java - Activate proximity sensor on button click -
i trying register proximity sensor listener when button clicked. nothing seems happen. ahve tried exploring internet of sort not anything. doing wrong? please help!
the xml file:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/pushupbg" android:orientation="vertical" > <textview android:id="@+id/tvtitlepu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:text="push counter" android:textcolor="#98f5ff" android:textsize="25sp" android:padding="10sp" android:textappearance="?android:attr/textappearancelarge" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/fragment1" android:layout_centerhorizontal="true" android:layout_marginbottom="20dp" android:orientation="horizontal" > <button android:id="@+id/bstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:typeface="serif" android:onclick="startcounter" android:text="start" /> <button android:id="@+id/bstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:typeface="serif" android:onclick="stopcounter" android:text="stop" /> <button android:id="@+id/bsave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:typeface="serif" android:onclick="savedata" android:text="save" /> </linearlayout> <textview android:id="@+id/tvcount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvtitlepu" android:layout_centerhorizontal="true" android:layout_margintop="22dp" android:text="" android:textcolor="#ffffff" android:textappearance="?android:attr/textappearancelarge" /> </relativelayout>
and here java file:
public class pushup_counter extends activity implements sensoreventlistener { sensormanager smgr; sensor sensor; button start,stop,save; textview title,count; int counter=0; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.pushup_layout); start=(button)findviewbyid(r.id.bstart); stop=(button)findviewbyid(r.id.bstop); save=(button)findviewbyid(r.id.bsave); title=(textview)findviewbyid(r.id.tvtitlepu); count=(textview)findviewbyid(r.id.tvcount); stop.setenabled(false); save.setenabled(false); } public void startcounter(view v) { // todo auto-generated method stub smgr=(sensormanager)getsystemservice(sensor_service); sensor=smgr.getdefaultsensor(sensor.type_proximity); smgr.registerlistener(this, sensor,sensormanager.sensor_delay_normal); stop.setenabled(true); } public void stopcounter(view v) { // todo auto-generated method stub smgr.unregisterlistener(this); save.setenabled(true); } public void savedata(view v) { // todo auto-generated method stub } @override public void onaccuracychanged(sensor arg0, int arg1) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent arg0) { // todo auto-generated method stub if (arg0.values[0] == 0){ counter++; count.settext("your count is: "+counter); } } }
logcat:
06-19 00:01:21.780: w/egl_genymotion(1493): eglsurfaceattrib not implemented 06-19 00:01:29.396: i/activitymanager(401): start u0 {cmp=com.example.fitgo/.pushup_counter} pid 1493 06-19 00:01:29.412: w/genymotion_audio(125): out_write() limiting sleep time 44149 23219 06-19 00:01:29.452: w/genymotion_audio(125): out_write() limiting sleep time 55759 23219 06-19 00:01:29.472: w/genymotion_audio(125): out_write() limiting sleep time 67369 23219 06-19 00:01:29.504: w/genymotion_audio(125): out_write() limiting sleep time 58979 23219 06-19 00:01:29.524: w/genymotion_audio(125): out_write() limiting sleep time 40589 23219 06-19 00:01:29.548: w/genymotion_audio(125): out_write() limiting sleep time 32199 23219 06-19 00:01:29.572: w/genymotion_audio(125): out_write() limiting sleep time 23809 23219 06-19 00:01:29.856: d/dalvikvm(401): gc_for_alloc freed 441k, 17% free 13865k/16596k, paused 25ms, total 25ms 06-19 00:01:29.884: d/dalvikvm(401): gc_for_alloc freed 748k, 19% free 13487k/16596k, paused 22ms, total 22ms 06-19 00:01:29.884: d/mobiledatastatetracker(401): default: setpolicydataenable(enabled=true) 06-19 00:01:30.152: w/egl_genymotion(1493): eglsurfaceattrib not implemented 06-19 00:01:30.360: i/activitymanager(401): displayed com.example.fitgo/.pushup_counter: +458ms 06-19 00:01:34.372: w/genymotion_audio(125): out_write() limiting sleep time 44149 23219 06-19 00:01:34.396: w/genymotion_audio(125): out_write() limiting sleep time 55759 23219 06-19 00:01:34.424: w/genymotion_audio(125): out_write() limiting sleep time 47369 23219 06-19 00:01:34.448: w/genymotion_audio(125): out_write() limiting sleep time 38979 23219 06-19 00:01:34.492: w/genymotion_audio(125): out_write() limiting sleep time 31178 23219 06-19 00:01:38.448: w/genymotion_audio(125): out_write() limiting sleep time 44149 23219 06-19 00:01:38.468: w/genymotion_audio(125): out_write() limiting sleep time 55759 23219 06-19 00:01:38.496: w/genymotion_audio(125): out_write() limiting sleep time 47369 23219 06-19 00:01:38.516: w/genymotion_audio(125): out_write() limiting sleep time 38979 23219 06-19 00:01:38.564: w/genymotion_audio(125): out_write() limiting sleep time 31178 23219
add onclicklistener start.setonclicklistener(startcounter)
or can way
bstart.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startcounter(v); } });
edit
let's make change on code see if it's code or not.
first of delete line of code :
android:onclick="startcounter"
then on pushup_counter
class change header :
public class pushup_counter extends activity implements sensoreventlistener, onclicklistener {
you have add on oncreate()
line of code :
start.setonclicklistener(this);
then have add method onclick()
looks :
@override public void onclick(view v) { // getid() returns view's identifier. switch(v.getid()){ case r.id.bstart; // todo auto-generated method stub smgr=(sensormanager)getsystemservice(sensor_service); sensor=smgr.getdefaultsensor(sensor.type_proximity); smgr.registerlistener(this, sensor,sensormanager.sensor_delay_normal); stop.setenabled(true); break; } }
comment rest of code if gives error, try button , let me know if works.
Comments
Post a Comment