Android 点击按钮即可访问接近传感器
我是android新手,我正在尝试通过点击一个按钮来实现接近传感器阻塞的计数[即接近传感器被阻塞的次数]。我不确定我在哪里犯了错误,因为我查阅了大部分可用的资料。如果我正在做任何事情,请指出同样的问题 错误Android 点击按钮即可访问接近传感器,android,proximity,Android,Proximity,我是android新手,我正在尝试通过点击一个按钮来实现接近传感器阻塞的计数[即接近传感器被阻塞的次数]。我不确定我在哪里犯了错误,因为我查阅了大部分可用的资料。如果我正在做任何事情,请指出同样的问题 错误 package com.fit.compaq.fitkit; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hard
package com.fit.compaq.fitkit;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class pushup extends AppCompatActivity
{
int count;
Sensor proximity;
SensorManager sm;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pushup);
ToggleButton pushupbutton = (ToggleButton) findViewById(R.id.tbutton_pushup);
pushupbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
text = (TextView) findViewById(R.id.text_pushup_counter);
final SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Toast.makeText(getApplicationContext(), "sensor changed:", Toast.LENGTH_SHORT).show();
text.setText(+event.values[0] + "");
if (event.values[0] < event.sensor.getMaximumRange()) {
count++;
Toast.makeText(getApplicationContext(), "Count = " + count, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
protected void onResume() {
sm.registerListener(this, proximity,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
sm.unregisterListener(this);
}
};
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pushup, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
package com.fit.compaq.fitkit;
导入android.content.Context;
导入android.content.Intent;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.widget.ToggleButton;
公开课俯卧撑活动
{
整数计数;
传感器接近度;
传感器管理器sm;
文本查看文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局、活动和俯卧撑);
ToggleButton pushupbutton=(ToggleButton)findViewById(R.id.tbutton\u pushup);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v)
{
sm=(SensorManager)getSystemService(Context.SENSOR\u服务);
接近度=sm.getDefaultSensor(传感器类型_接近度);
text=(TextView)findViewById(R.id.text\u pushup\u计数器);
最终SensorEventListener mySensorEventListener=新的SensorEventListener(){
@凌驾
传感器更改时的公共无效(传感器事件){
Toast.makeText(getApplicationContext(),“传感器已更改:”,Toast.LENGTH_SHORT.show();
text.setText(+event.values[0]+“”);
if(event.values[0]
}添加一些代码,以便我们可以帮助您。到目前为止,我们甚至不知道错误在哪里。我没有得到任何错误,只是在点击调用接近传感器的按钮后没有得到任何传感器值。