How to open browser from android cocos2D Application?

By | February 7, 2012

This code snippet helps you to open browser in android cocos2D application

public void openBrowser(){
		CCDirector.sharedDirector().getActivity().runOnUiThread(new Runnable() {
		    public void run() {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
				CCDirector.sharedDirector().getActivity().startActivity(browserIntent);
		}});
	}

Call this function anywhere inside your cocos2D project to open the browser with the corresponding URL.

2 thoughts on “How to open browser from android cocos2D Application?

  1. David

    Excuse me, I implemented this, but I would like to know how can I do it so that when I press the phone’s back button it goes back to the cocos2d layer that called the openBrowser method, when I press it, it restarts the game from the beginning

    Reply
    1. James Post author

      IF you have opened the browser from a cocos2DLayer then clicking on back button will bring you back to the previous cocos2d layer. Please check if you are not destroying the activity then the onPause() function is called on the activity when your browser is opened, this may be causing the game to restart.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *