【问题标题】:Cannot open Android Webbrowser using Action_View无法使用 Action_View 打开 Android Webbrowser
【发布时间】:2016-04-24 06:51:11
【问题描述】:

我正在尝试通过单击文本视图来打开标准的 android 网络浏览器。 我在 textview 中定义了 android:autoLink="web" 然后使用 onTouchListener 来启动 browserintent:

// On Touch Listener

chatText.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                      // view.performClick();
                       view.onTouchEvent(event);

                       if (event.getAction() == MotionEvent.ACTION_DOWN){
                           view.performClick();
                           openBrowser(chatText.getText().toString());
                       }
                return false;
            }
        });

 // Start Browser function

 public void openBrowser(String url) {
    Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (webIntent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(webIntent);
    }
}

但是,每次我点击我的文本视图上的链接时,我都会收到错误:

从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?

虽然我在新开始的活动中添加了标志,但有人知道我在这里做错了什么吗?

我从扩展 ArrayAdapter 的 ChatArrayAdapter 类调用活动,但是,我将适当的上下文传递给新启动的活动

【问题讨论】:

    标签: android android-intent android-browser


    【解决方案1】:

    查看您的代码,您的聊天文本中有一些 URL。为了让事情更简单,Android 有Linkify 类和android:autoLink XML 属性。这些有助于在您单击链接时自动突出显示链接并执行标准操作。所有这些都是开箱即用的。

    您的chatText 可能是TextView。您可以使用我提到的两种方法之一:

    在 XML 中:
    在您的 chatText 的 XML 中添加以下属性:

    android:autoLink="web"  
    

    在代码中:

    Linkify.addLinks(chatText,Linkify.WEB_URLS);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2023-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      相关资源
      最近更新 更多