考虑以下代码:
package com.example.savag.bcapplication;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.Settings;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener{
//define global variable types
int counter=0;
String strcounter;
int buttoncounter=0;
String strbcounter;
TextView dynamicheader;
//Define the sensor Manager
SensorManager sm;
//Define the Motion Sensor objects
Sensor accelerometer;
Sensor gravity;
Sensor gyroscope;
Sensor uncalgyro;
Sensor lineaccel;
Sensor rotatevector;
Sensor sigmotion;
Sensor stepcounter;
Sensor stepdetector;
//Define the changing Motion Sensor text values
TextView gyrosense;
TextView uncalgyrosense;
TextView acceleration;
TextView gravitysense;
TextView lineaccelsense;
TextView rotatesense;
TextView sigmotionsense; //In order to make use of this, use onTrigger event
TextView stepstaken;
TextView stepdetected; //In order to make use of this, use onTrigger event
//Define the position sensor objects
Sensor gamerotatevector;
//Define the changing Position sensor objects
TextView gamerotatesense;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// variables final static
final ImageButton button1 = (ImageButton)findViewById(R.id.motbut_unpressed);
final ImageView topbar2 = (ImageView)findViewById(R.id.topbarmain);
final ImageButton button2 = (ImageButton)findViewById(R.id.posbut_unpressed);
final ImageButton button3 = (ImageButton)findViewById(R.id.envbut_unpressed);
final LinearLayout babcockandfield = (LinearLayout)findViewById(R.id.babcocklayout);
final ImageButton arrowup = (ImageButton)findViewById(R.id.uparrow);
final ImageButton arrowdown = (ImageButton)findViewById(R.id.downarrow);
final ImageView navbar = (ImageView)findViewById(R.id.infospace);
//Global counter variables
counter = 0;
strcounter = Integer.toString(counter);
buttoncounter = 0;
strbcounter = Integer.toString(buttoncounter);
//Set Nav bar invisible for Now
arrowup.setVisibility(View.GONE);
arrowdown.setVisibility(View.GONE);
navbar.setVisibility(View.GONE);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
HandlerThread mHandlerThread = new HandlerThread("sensorThread");
mHandlerThread.start();
final Handler handler = new Handler(mHandlerThread.getLooper());
button1.setonClickListener( //This is for the motion button
new ImageButton.OnClickListener() {
public void onClick(View v) {
buttoncounter=1;
strbcounter = Integer.toString(buttoncounter); // maybe need and on counter listener
//change colour of pressed button
button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_pressed, null));
topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarmotion, null));
bcandf.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbarmotion, null));
//make sure the other two buttons are normal again
button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_normal, null));
button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_normal, null));
//make the arrows and infospace appear
arrowup.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.VISIBLE);
navbar.setVisibility(View.VISIBLE);
//Dynamic text appearance
//set the text inside infospace
//dynamicheader.setVisibility(View.VISIBLE);
}
}
);
button2.setonClickListener( //This is for the position button
new ImageButton.OnClickListener(){
public void onClick(View v){
buttoncounter=2;
strbcounter = Integer.toString(buttoncounter); // maybe need and on counter listener
//Change colour of pressed button
button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_pressed, null));
topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarposition, null));
bcandf.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbaralternate, null));
//make sure the other two buttons are normal again
button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_normal, null));
button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_normal, null));
//make the arrows and the infospace appear
arrowup.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.VISIBLE);
navbar.setVisibility(View.VISIBLE);
//Dynamic text appearance
//set the text inside infospace
//dynamicheader.setVisibility(View.VISIBLE);
}
}
);
button3.setonClickListener( //This is for the environment button
new ImageButton.OnClickListener(){
public void onClick(View v){
buttoncounter=3;
strbcounter = Integer.toString(buttoncounter); // maybe need and on counter listener
//change colour of pressed button
button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_pressed, null));
topbar2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.topbarenviro, null));
bcandf.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.logoandbaralternate, null));
//make sure the other two buttons are normal again
button1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.motionbutton_normal, null));
button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_normal, null));
//make the arrows and the infospace appear
arrowup.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.VISIBLE);
navbar.setVisibility(View.VISIBLE);
//Dynamic text appearance
//set the text inside infospace
// dynamicheader.setVisibility(View.VISIBLE);
}
}
);
arrowup.setonClickListener( //This is for the up arrow
new ImageButton.OnClickListener(){
public void onClick(View v){
if (strcounter.equals("0")||(strcounter.equals("10")))
{
//do nothing if strcounter is 0
}
else
{
counter--; //how many times
strcounter = Integer.toString(counter);
//et1.setText(strcounter);
//Now lets set text accordingly
//First we need to set in sequential order and also check button we are on
if (strcounter.equals("1")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion {
//Motion button going on
gravitysense.setVisibility(View.GONE);
acceleration.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("acceleration:");
//Now we need to register the sensors and whatnot
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_norMAL, handler);
//acceleration.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
}
}
if (strcounter.equals("2")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion {
//Get rid of the last values and set the one we are using
acceleration.setVisibility(View.GONE);
gravitysense.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Gravity Sensor:");
//gravitysense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
}
}
if (strcounter.equals("3")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion
//Get rid of the last values and set the one we are using
gravitysense.setVisibility(View.GONE);
gyrosense.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("gyroscope:");
//gyrosense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
}
}
if (strcounter.equals("4")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion
//Get rid of the last values and set the one we are using
gyrosense.setVisibility(View.GONE);
uncalgyrosense.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Raw gyroscope:");
//uncalgyrosense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2] +
// "\nX: " + event.values[3] + "\nY: " + event.values[4] + "\nZ: " + event.values[5]);
}
}
if (strcounter.equals("5")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion
uncalgyrosense.setVisibility(View.GONE);
lineaccelsense.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Linear Sensor:");
//lineaccelsense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
}
}
if (strcounter.equals("6")) {
////////
if (strbcounter.equals("1")) { // Check if motion has been pressed
lineaccelsense.setVisibility(View.GONE);
rotatesense.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Rotation Sensors");
//rotatesense.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
}
}
if (strcounter.equals("7")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion
rotatesense.setVisibility(View.GONE);
sigmotionsense.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Significant Motion Sensor:");
// sigmotionsense.setText(event.values[0] + "");
}
}
if (strcounter.equals("8")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion
sigmotionsense.setVisibility(View.GONE);
stepstaken.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Step Counter:");
}
}
if (strcounter.equals("9")) {
////////
if (strbcounter.equals("1")) { // Check which button 1 is motion
stepstaken.setVisibility(View.GONE);
stepdetected.setVisibility(View.VISIBLE);
//set the text inside infospace
dynamicheader.setText("Step Detector:");
}
}
}
}
}
);
arrowdown.setonClickListener( //This is for the down arrow
new ImageButton.OnClickListener(){
public void onClick(View v){
if (strcounter.equals("9")||(strcounter.equals("0"))) // might be that we need to add a second variable for mot,pos,env
{
//Do nothing as we have reached the bottom
}
else
{
counter++; //how many times
strcounter = Integer.toString(counter);
//et1.setText(strcounter);
//Now lets set text accordingly
if (strcounter.equals("8")) {
////////
stepstaken.setVisibility(View.VISIBLE);
stepdetected.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("Steps Taken:");
}
if (strcounter.equals("7")) {
////////
sigmotionsense.setVisibility(View.VISIBLE);
stepstaken.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("Significant Motion:");
}
if (strcounter.equals("6")) {
////////
rotatesense.setVisibility(View.VISIBLE);
sigmotionsense.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("Rotation Sensor:");
}
if (strcounter.equals("5")) {
////////
lineaccelsense.setVisibility(View.VISIBLE);
rotatesense.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("Linear acceleration:");
}
if (strcounter.equals("4")) {
////////
uncalgyrosense.setVisibility(View.VISIBLE);
lineaccelsense.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("Raw gyroscope:");
}
if (strcounter.equals("3")) {
////////
gyrosense.setVisibility(View.VISIBLE);
uncalgyrosense.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("gyroscope:");
}
if (strcounter.equals("2")) {
////////
gravitysense.setVisibility(View.VISIBLE);
gyrosense.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("Gravity Sensor:");
}
if (strcounter.equals("1")) {
////////
acceleration.setVisibility(View.VISIBLE);
gravitysense.setVisibility(View.GONE);
//set the text inside infospace
dynamicheader.setText("acceleration:");
}
}
}
});
}
@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_main, 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);
}
@Override
public void onSensorChanged(SensorEvent event) {
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
该应用程序基本上会监听一堆按钮,并为基于按钮的自适应UI设置一些可见和不可见等.
下一步是节省内存,以便SensorEventListener不会在封装在OnClick方法中的同时监听每个传感器.
目的是在点击时注册和注销.
第一个箭头注册加速度计,然后下一个箭头将取消注册加速度计并注册另一个传感器.
然后在SensorChanged中获取事件信息以进行显示.
首先,我尝试没有任何处理程序,只是这样写:
sm.registerListener.(this, accelerometer, SensorManager.SENSOR_DELAY_norMAL);
Cannot resolve method 'registerListener(anonymous
android.view.View.OnClickListener, android.hardware.Sensor, int)'
所以我认为也许需要一个处理程序,所以如更新的代码所示,我添加了一个处理程序,因此需要将其设置为Final,因为android studio标记了错误.因此,我添加了您可以在OnCreate方法下看到的处理程序的代码.插入处理程序会显示以下内容:
Cannot resolve method 'registerListener(anonymous
android.view.View.OnClickListener, android.hardware.Sensor, int,
android.os.Handler)'
我不确定为什么这么难做到.我希望传感器管理器也只在需要时监听我需要的传感器.对此问题的任何帮助或澄清将不胜感激.
解决方法:
使用MainActivity.this代替它作为registerListener和unregisterListener中的第一个参数.
sm.registerListener.(MainActivity.this, accelerometer,
SensorManager.SENSOR_DELAY_norMAL);
原因是onClick方法内部,它引用onClick方法的上下文,而不是在其中实现SensorEventListener的MainActivity
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。