1

I'm kind of new to java and in my test project I encountered a problem.
How can I return value from a void method? If I change the title, I receive this error :
"attempting to use incompatible return type" here is the code

public void show_sensor (Context appcontext, int sensortype){

    new ReactiveSensors(appcontext).observeSensor(sensortype)
            .subscribeOn(Schedulers.computation())
            .filter(ReactiveSensorFilter.filterSensorChanged())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<ReactiveSensorEvent>() {
                @Override
                public void call(ReactiveSensorEvent reactiveSensorEvent) {
                    SensorEvent event = reactiveSensorEvent.getSensorEvent();

                    float x = event.values[0];
                    float y = event.values[1];
                    float z = event.values[2];

                    String message = String.format("x = %f, y = %f, z = %f", x, y, z);

                    Log.d("gyroscope readings", message);
                }
            });
}

I want to return "message" is there any way i can do this?
And I also have a problem getting application context, so I had to pass it on, is it possible to get ApplicationContext from within the class so I can run it in the background?
(i want to set up a broadcast and i cant use "getApplicationContext()" in the receiver class)

Michał Szewczyk
  • 7,540
  • 8
  • 35
  • 47
Omid
  • 438
  • 4
  • 11
  • 4
    you can't. If you override a method, the returned type must be the same. – jhamon Sep 21 '16 at 12:27
  • The method `call(..)` is executed asynchronously by an unknown method. Therefore returning a String (to that unknown method) won't help you here. – Robert Sep 21 '16 at 12:41

4 Answers4

3

its impossible, if you override an method you are promising that the return type will stay the same, so you cant.

EDIT: just noticed you are saving values from message variable into somekind of a log file, try accesing that log file from another function and getting the message, might be the easiest solution.

Luka Špoljarić
  • 988
  • 1
  • 10
  • 23
  • 1
    well its android log. just did it to check if my functions are working correctly, and i don`t think if i can access it from the app. but it gave me an idea. i can write data to file and read it whenever i want. – Omid Sep 21 '16 at 12:59
1

The best way to return the variable is using an interface to communicate the classes.

Reference

Java Reference

Thy use eventbus: Eventbus

Step 1. In gradle:

compile 'org.greenrobot:eventbus:3.0.0'

Step 2. Define message

public class MessageEvent {

public final String message;

public MessageEvent(String message) {
    this.message = message;
    }
}

Step 3: Prepare subscribers:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
   Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}

Step 4: Register subscribers

@Override
public void onStart() {
  super.onStart();
  EventBus.getDefault().register(this);
}

@Override
public void onStop() {
   EventBus.getDefault().unregister(this);
   super.onStop();
}

Final Step: Dispatch event:

String message = String.format("x = %f, y = %f, z = %f", x, y, z);
EventBus.getDefault().post(new MessageEvent (message));
Log.d("gyroscope readings", message);
Community
  • 1
  • 1
0

No, we can't return a value from void method.

Use String as return type rather void, so that you can receive message back to caller method.

If the method is overridden, we can't change the return type of that method.

-1

set the result to a global variable then access that variable.

sajan
  • 389
  • 5
  • 11
  • well its a method that im gonna use for few sensors. i think that makes problems. unless it stays inside the class – Omid Sep 21 '16 at 13:12
  • Global variables are not considered as a good practice because they make code harder to understand. And in the case with asynchronous calls things even go worse because you have to track when the global variable has been changed in another thread. – Alexey Guseynov Sep 21 '16 at 16:14