Android:将我的WebView活动添加到Chooser Web浏览器

时间:2018-02-05 10:53:18

标签: android webview

我有一个应用程序(1个XML和1个活动):

  1. 用于填写网址的编辑文字。
  2. 用于打开网络浏览器的按钮。 My home screen app
  3. 我的活动:

    public class ActivityBai2 extends AppCompatActivity {
    
    private Button btnMoTrinhDuyet;
    private EditText editTextLink;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bai2);
    
        btnMoTrinhDuyet = findViewById(R.id.buttonMoTrinhDuyet);
        editTextLink = findViewById(R.id.editLink);
    
        btnMoTrinhDuyet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = editTextLink.getText().toString();
                if(!url.equals("")) {
                    if(!url.startsWith("http://") && !url.startsWith("https://")) {
                        url = "http://" + url;
                    }
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(ActivityBai2.this, "Vui lòng nhập địa chỉ!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    
    }
    

    }

    我创建了一个包含WebView的XML(以及一个显示WebView的活动)。

    public class ActivityBai3 extends AppCompatActivity {
    
    private WebView myWebView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bai3);
    
        Intent i = getIntent();
        Uri data = i.getData();
        URL url = null;
    
        myWebView = findViewById(R.id.webView);
        try {
            url = new URL(data.getScheme(), data.getHost(), data.getPath());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        myWebView.loadUrl(url.toString());
    
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    
    
    }
    

    }

    单击“按钮”时,如何将此Web视图添加到“选择器”Web浏览器窗口中。

    这是第一个XML:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Nhập đường link trang web"
            android:textAlignment="center"
            android:textSize="18sp" />
    
        <EditText
            android:id="@+id/editLink"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:layout_marginTop="30dp"
            android:textAlignment="center" />
    
        <Button
            android:id="@+id/buttonMoTrinhDuyet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:text="mở trình duyệt"
            android:textSize="18sp" />
    </LinearLayout>
    

    谢谢!

    我将此添加到AndroidManifest.xml并且它可以正常工作。

    <activity android:name=".ActivityBai2" android:label="home screen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ActivityBai3" android:label="My Web Browser">
            <intent-filter tools:ignore="AppLinkUrlError">
                <action android:name="android.intent.action.VIEW"/>
                <data android:scheme="http"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>
    

1 个答案:

答案 0 :(得分:0)

要允许其他应用访问您的活动,您必须在活动中添加意图过滤器。

Allowing Other Apps to Start Your Activity