在动态壁纸中启动浏览器意图

3

我正在尝试在Android的onTouchEvent中加载浏览器视图意图。基本上,我已经创建了一个动态壁纸,如果我点击它,我想打开带有指定URI的浏览器视图意图。

我已经尝试使用以下代码:

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

但是我遇到了以下错误:
android.util.AndroidRuntimeException: Calling startActivity() from outside of 
an Activitycontext requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

另一种方法是我创建了一个活动,在其onCreate方法中,我尝试了以下代码:
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

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

我尝试通过自定义意图消息加载此活动,如下所示,但仍然遇到相同的错误。

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
1个回答

5
我已经用以下代码自己解决了问题:
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);

1
这对我解决了问题,但我想了解为什么需要这样做;Android文档似乎暗示应该可以在当前任务中打开浏览器活动。有人能解释一下吗? - Symmetric
刚刚得到一个答案-捕获异常会显示以下信息:"从Activity外部调用startActivity()需要使用FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?"。 - Symmetric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接