按下按钮激活接近传感器

时间:2015-06-18 17:49:40

标签: java android button sensor proximity

我正在尝试仅在单击按钮时注册接近传感器侦听器。但似乎没有任何事情发生。我曾经尝试过在互联网上寻找类似的东西,却无法得到任何东西。 我究竟做错了什么?请帮忙!

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>

这是 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的:

&#13;
&#13;
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
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

像这样start.setOnClickListener(startCounter)

添加onClickListener

或者你也可以这样做

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

修改

让我们对您的代码进行更改,看看它是否与您的代码有关。

首先删除这行代码:

android:onClick="startCounter"

然后在PushUp_Counter班级上将标题更改为:

public class PushUp_Counter extends Activity implements SensorEventListener, OnClickListener  {

您必须在onCreate()这行代码中添加:

start.setOnClickListener(this);

然后你必须添加如下所示的方法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;
    }
}

如果它给你任何错误,请注释其余代码,只需尝试此按钮并告诉我它是否有效。

答案 1 :(得分:0)

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

start.setOnClickListener(  startCounter);

答案 2 :(得分:0)

传感器监听器似乎在onResume()方法中注册时有效。 我所做的是让传感器监听器激活,但在点击按钮之前不启动计数器。单击该按钮后,布尔变量将变为 true ,并在onSensorChanged()中进行检查。

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>

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);
	}
	}
}}

我希望这能帮助像我这样的人。