简体   繁体   English

单击按钮激活接近传感器

[英]Activate proximity sensor on button click

I am trying to register the proximity sensor listener only when a button is clicked. 我试图仅在单击按钮时注册接近传感器侦听器。 But nothing seems to happen. 但是似乎什么也没发生。 I ahve tried exploring the internet for something of the sort but could not get anything. 我曾尝试过在互联网上进行某种搜索,但无法获得任何信息。 What am I doing wrong? 我究竟做错了什么? Please help! 请帮忙!

The xml file : xml文件

 <?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 Up 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 is the java file : 这是java文件

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: 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} from pid 1493 06-19 00:01:29.412: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219 06-19 00:01:29.452: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219 06-19 00:01:29.472: W/genymotion_audio(125): out_write() limiting sleep time 67369 to 23219 06-19 00:01:29.504: W/genymotion_audio(125): out_write() limiting sleep time 58979 to 23219 06-19 00:01:29.524: W/genymotion_audio(125): out_write() limiting sleep time 40589 to 23219 06-19 00:01:29.548: W/genymotion_audio(125): out_write() limiting sleep time 32199 to 23219 06-19 00:01:29.572: W/genymotion_audio(125): out_write() limiting sleep time 23809 to 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 to 23219 06-19 00:01:34.396: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219 06-19 00:01:34.424: W/genymotion_audio(125): out_write() limiting sleep time 47369 to 23219 06-19 00:01:34.448: W/genymotion_audio(125): out_write() limiting sleep time 38979 to 23219 06-19 00:01:34.492: W/genymotion_audio(125): out_write() limiting sleep time 31178 to 23219 06-19 00:01:38.448: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219 06-19 00:01:38.468: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219 06-19 00:01:38.496: W/genymotion_audio(125): out_write() limiting sleep time 47369 to 23219 06-19 00:01:38.516: W/genymotion_audio(125): out_write() limiting sleep time 38979 to 23219 06-19 00:01:38.564: W/genymotion_audio(125): out_write() limiting sleep time 31178 to 23219 

Add the onClickListener like this start.setOnClickListener(startCounter) 像这样添加start.setOnClickListener(startCounter)

Or also you can do it by this way 或者你也可以这样

bStart.setOnClickListener(new View.OnClickListener() {
@Override
   public void onClick(View v) {
    startCounter(v);
   }
});

EDIT 编辑

Let's make a change on your code to see if it's about your code or not. 让我们对您的代码进行更改,以查看它是否与您的代码有关。

First of all delete this line of code : 首先删除以下代码行:

android:onClick="startCounter"

Then on your PushUp_Counter class change the header to this : 然后在您的PushUp_Counter类上,将标头更改为:

public class PushUp_Counter extends Activity implements SensorEventListener, OnClickListener  {

You have to add on your onCreate() this line of code : 您必须在onCreate()上添加以下代码行:

start.setOnClickListener(this);

Then you have to add the method onClick() that looks like : 然后,您必须添加如下所示的onClick()方法:

@Override
public void onClick(View v) { 
    // getId() returns this 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 the rest of code if it gives you any error, just try this button and let me know if it works. 如果其余代码有任何错误,请注释其余的代码,只需尝试此按钮,让我知道它是否有效。

您必须在onCreateView()方法中分配onClick侦听器

start.setOnClickListener(  startCounter);

The sensor listener seems to work when registered in the onResume() method. onResume()方法中注册后,传感器侦听器似乎可以工作。 What i have done is let the sensor listener be activated but not start the counter until the button is clicked. 我所做的是让传感器侦听器被激活,但是直到单击按钮后才启动计数器。 As soon as the button is clicked, a boolean variable turns true , which is checked in onSensorChanged() . 单击按钮后,布尔变量将变为true ,这将在onSensorChanged()进行检查。

The xml: xml:

 <?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" > <fragment android:id="@+id/fragment1" android:name="com.example.fitgo.Frag_Pushup" android:layout_width="wrap_content" android:layout_height="250dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" /> <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 Up 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:onClick="startCounter" android:typeface="serif" 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> 

The java file: Java文件:

 public class PushUp_Counter extends Activity implements SensorEventListener { private SensorManager smgr; private Sensor sensor; Button start,stop,save; TextView title,count; int counter=0; boolean check; @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); check=false; smgr=(SensorManager)getSystemService(SENSOR_SERVICE); sensor=smgr.getDefaultSensor(Sensor.TYPE_PROXIMITY); stop.setEnabled(false); save.setEnabled(false); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); smgr.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); smgr.unregisterListener(this); } public void startCounter(View v) { // TODO Auto-generated method stub check=true; stop.setEnabled(true); } public void stopCounter(View v) { // TODO Auto-generated method stub check=false; 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(check==true) { if (arg0.values[0] == 0){ counter++; count.setText("Your count is: "+counter); } } }} 

I hope this would help someone like me. 我希望这会对像我这样的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM