Search code examples
javaandroidbrowsergame-physics

Android Development: Opening link in browser without using Activity


I'm developing a game on Android. On the Main Menu Screen, I wish to provide a link to my website.. This screen is an image, which has touch events for carrying out different actions. The link to my website should also be a result of a touchEvent.

My MainMenuScreen.java extends another java file, and cannot extend Activity.

Can you please suggest how I can redirect the user to the website, using a touch event, but without making it an activity.

Here's a section of the code within my MainMenuScreen.java (here the extended Screen is an abstract class defined by me, which is used to extend all screens in the game)

public class MainMenuScreen extends Screen {
public MainMenuScreen(Game game) {
    super(game);
}

@Override
public void update(float deltaTime) {
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();
    for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);
        if (event.type == TouchEvent.TOUCH_UP) {

            if (inBounds(event, 50, 300, 250, 450)) {
                game.setScreen(new GameScreen(game));
            }
            if(inBounds(event, 370, 300, 600, 450)) {
                // Needs the code to redirect user to website
            }
        }
    }
}

Please help


Solution

  • Assuming you have a reference to the android.view.View that draws your Screen (or your class Screen extends View), you can do this:

    Context context = view.getContext(); // or Context context = getContext();
    if (context instanceof Activity) {
        Activity activity = (Activity) context;
    
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
        activity.startActivity(intent);
    }