Consider this code:
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) {
}
}
The app basically listens to a bunch of buttons, and sets some visible and invisible etc, for adaptive UI based on buttons.
Next to save memory, and so that the SensorEventListener isn't listening to every single sensor at the same time it's enclosed within a OnClick method.
The intention is to register and unregister on click.
First arrow registers accelerometer, then the next arrow will unregister accelerometer and register another sensor.
Then in SensorChanged grab the event info for display.
First of all I tried without any handler and just wrote like this:
sm.registerListener.(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
This flagged up the following error:
Cannot resolve method 'registerListener(anonymous
android.view.View.OnClickListener, android.hardware.Sensor, int)'
So I thought that perhaps it needs a handler, so as the updated code shows I added an handler, this needed to be set as Final, because android studio flagged up an error. So I added the code for the handler that you can see under the OnCreate method. Inserting the handler brought up this:
Cannot resolve method 'registerListener(anonymous
android.view.View.OnClickListener, android.hardware.Sensor, int,
android.os.Handler)'
I'm unsure why this is so difficult to do. I want the sensor manager to only listen to the sensor that I need it too, if and when I select one. Any help or clarification on the matter would be greatly appreciated.
Use MainActivity.this instead of this as first parameter in registerListener and unregisterListener .like:
sm.registerListener.(MainActivity.this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
reason is inside onClick method this refer to the context on onClick method instead of MainActivity in which you are implementing SensorEventListener
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With