0

I have two public voids in a class called Flashlight, these are:

public void turnOnFlashLight()

public void turnOffFlashLight()

How can I access these within Main Activity?

I've found many guides for using another class in MainActivity but not for accessing only a specific part (in this case I'm trying to turn the flashlight on or off).

This is he contents of MainActivity

public class MainActivity extends android.support.v7.app.ActionBarActivity {

@Override
protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    httpserver();

public void httpserver() {
    AsyncHttpServer server = new AsyncHttpServer();
    java.util.List<WebSocket> _sockets = new java.util.ArrayList<WebSocket>();
    server.listen(5000);
    server.get("/flashon", new HttpServerRequestCallback() {
        static final int CAMERA_PIC_REQUEST = 0;
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            response.send("Ok");
            Flashlight.turnOnFlashLight();

        }
    });
}
}

This is the Flashlight class https://stackoverflow.com/a/31289731/6696740

Thank you :)

Community
  • 1
  • 1
Tiffany
  • 101
  • 4

2 Answers2

0

Make sure that class is imported in MainActivity. You should be able to just use Flashlight.turnOnFlashLight() or Flashlight.turnOffFlashLight(). You might also want to create an instance of the Flashlight class like Flashlight flashlight = Flashlight.newInstance(), then flashlight.turnOnFlashLight() or Flashlight.turnOffFlashLight().

Jeffrey J
  • 53
  • 2
  • 8
  • wow that was quick, thank you! How could do this if the class is not and cannot be set to static? I get "Cannot be referenced from a static context". – Tiffany Aug 11 '16 at 16:56
  • This is the Flashlight class code: http://stackoverflow.com/a/31289731/6696740 this is where I am trying to use it in MainActivity public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { response.send("Ok"); Flashlight.turnOnFlashLight(); } – Tiffany Aug 11 '16 at 17:07
  • Can you post your MainActivity.java? – Jeffrey J Aug 11 '16 at 17:11
  • You cannot simply write 'Flashlight.turnOnFlashLight()' because methods are not market as static. – Mahdi Aug 11 '16 at 17:13
  • I've added the contents of main activity (excluding the imports) in the main question. – Tiffany Aug 11 '16 at 17:16
0

In MainActivity (Assuming FlashLight class constructor does not need any input):

import Flashlight;
...
//where you need to call those methods
Flashlight flashLight = new Flashlight();
flashlight.turnOnFlashLight();
//OR
flashlight.turnOffFlashLight();
Mahdi
  • 1,871
  • 2
  • 24
  • 41