0

for example i have made a Nodemcu server and there are 3 links on the main webpage(192.168.18.100). the thing i want to try is that when i click an android button this link get accessed without opening any new activity i.e i want to stay on the main activity. this is my code so far that works but not according to what i want it.

public class MainActivity extends AppCompatActivity {

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

    Button on = (Button) findViewById(R.id.button);
    Button off =(Button) findViewById(R.id.button2);
    Button pink = (Button) findViewById(R.id.button3);

    on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_SEND, uri);
            startActivity(intent);
        }
    });
    off.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
    pink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
}

}

1 Answers1

0

You can try to implement a WebView on your MainActivity. When you click on any of your button, load the WebView with your desired url.

You can find the way to implement WebView in this link

Hellious
  • 180
  • 9
  • i dont want to show anything to the user i just want to access a link without showing it for example when the link is triggered or accessed using a browser it turns on the led in my home – Muhammad Ahmad Jul 06 '21 at 03:40
  • It possibly a duplicate from [How to execute URL in Android?](https://stackoverflow.com/questions/29458365/how-to-execute-url-in-android) or [Hit a URL without opening browser in Android Studio](https://stackoverflow.com/questions/42294863/hit-a-url-without-opening-browser-in-android-studio) – Hellious Jul 06 '21 at 03:50