【问题标题】:launch browser intent in live wallpaper在动态壁纸中启动浏览器意图
【发布时间】:2026-02-15 19:45:01
【问题描述】:

我正在尝试在onTouchEvent 内的android 中加载BROWSER VIEW 意图。基本上我已经创建了一个动态壁纸,如果我点击它然后我想用指定的 uri 打开 BROWSER VIEW 意图。

我在onTouchEvent中尝试了以下代码

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

【问题讨论】:

    标签: android live-wallpaper wallpaper


    【解决方案1】:

    我已经用下面的代码自己解决了这个问题:

    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);
    

    【讨论】:

    • 这解决了我的问题,但我想了解为什么这是必要的; Android 文档似乎暗示应该可以在当前任务中打开浏览器活动。谁能解释一下?
    • 刚刚得到了答案——捕获异常会给出以下消息:“从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?”。