3

I am trying to load BROWSER VIEW intent in android within onTouchEvent. Basically I have created a live wallpaper and if I click on it then I want to open BROWSER VIEW intent with a specified uri.

I have tried following code inside onTouchEvent

Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

but I am getting the following error:

android.util.AndroidRuntimeException: Calling startActivity() from outside of 
an Activitycontext requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Another approach is that I have create one activity and inside its onCreate method I tried following code:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    browser=new WebView(this);
    setContentView(browser);
    browser.loadUrl("http://commonsware.com");
}

and tried to load this activity via custom intent message as follows, but still I am getting the same error

Intent intent = new Intent(ACTION);
startActivity(intent);

// over here I have not used context, as while creating live wallpaper I
// don't know here to get the context
sarnold
  • 102,305
  • 22
  • 181
  • 238
Hunt
  • 8,215
  • 28
  • 116
  • 256

1 Answers1

5

I have solved the problem on my own with the following code:

Intent intent = new Intent(Intent.ACTION_VIEW);                  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(android.net.Uri.parse("http://www.gmail.com"));
startActivity(intent);
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Hunt
  • 8,215
  • 28
  • 116
  • 256
  • 1
    This fixed the problem for me, but I'd like to understand why this is necessary; the Android docs seem to imply that it should be possible to open the browser Activity within the current Task. Can anyone explain this? – Symmetric Jan 28 '11 at 06:08
  • Just got an answer to this -- catching the exception gives the following message: "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?". – Symmetric Jan 28 '11 at 06:30