简体   繁体   English

在WebView Android中检测到URL时如何更改活动

[英]How to change activity when an url is detect in webview Android

I try to change Activity when an URL is detected in webview in my Android app. 当我在Android应用中的webview中检测到URL时,我尝试更改“ Activity I get this error: 我收到此错误:

Unable to start activity ComponentInfo{"xxx"}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=Open.Tok.Act }

Here is my AndroidManifest.xml 这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dk.westsoftdevelopment.elaegen" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!-- Using the camera -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="a"
        android:theme="@style/AppTheme"
        >
        <activity android:name=".OpenTokActivity" android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="Open.Tok.Act" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".WebViewActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here is my code for my WebView : 这是我的WebView代码:

aWebView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        if(url.equals("http://95.85.53.176/nhi/booktid"))
        {
            Log.d(LOGTAG, "GO TO OPENTOK!!!");
            startActivity(new Intent("OpenTokAct"));
        }
        Log.d(LOGTAG,"current URL = " + url);

    }



});

aWebView.loadUrl(mainUrl);

I Have also tried this in the beginning of onCreate() , but it gives me same error... 我也在onCreate()的开头尝试过此方法,但它给了我同样的错误...

Intent openStartingPoint = new Intent("Open.Tok.Act");
startActivity(openStartingPoint);

Thank you 谢谢

Try this, 尝试这个,

Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);

When firing an Intent , you have two choices: to make it explicit or implicit . 触发Intent ,有两种选择:使其显式隐式

Right now, you're doing the latter. 现在,您正在做后者。 In order for that to work, your OpenTokActivity 's intent filter will need to be updated accordingly: 为了使其正常工作,您的OpenTokActivity的意图过滤器将需要相应地更新:

<intent-filter>
    <action android:name="Open.Tok.Act" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" /> <!-- optional -->
</intent-filter>`

Why? 为什么?

  • The DEFAULT category is required for the Context.startActivity() method to resolve your activity when its component name is not explicitly specified. 如果未明确指定其组件名称,则Context.startActivity()方法需要DEFAULT类别才能解析您的活动。
  • The BROWSABLE category should be added to activities that can be safely invoked from a browser. 应将BROWSABLE类别添加到可以从浏览器安全调用的活动中。

Note that since you're creating the Intent yourself, you don't need the 'browsable' category. 请注意,由于您是自己创建Intent ,因此不需要“可浏览”类别。 If, at some later stage, you would want the website to automatically trigger a redirect that opens the activity in your app, then you'll need to add it back in. Above is all documented here . 如果在以后的某个阶段,您希望网站自动触发重定向以打开您的应用程序中的活动,那么您需要将其重新添加。以上已在此处记录

However, unless you're going to have more than one activity that can handle the custom action, it is going to be a lot easier to just fire an explicit intent: 但是,除非您将要拥有多个可以处理自定义动作的活动,否则只需发出一个明确的意图就容易多了:

startActivity(view.getContext(), OpenTokActivity.class);

You can then remove the whole <intent-filter> ... </intent-filter> part of OpenTokActivity , unless you deliberately added the LAUNCHER category (which will add an entry point for the activity in the top-level launcher). 然后,您可以删除OpenTokActivity的整个<intent-filter> ... </intent-filter>部分,除非您有意添加了LAUNCHER类别(这将在顶级启动器中添加该活动的入口点)。

Different ways of starting activities in android. 在Android中启动活动的不同方式。

First Way : - Explicit 第一种方式:-明确

Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);

Second Way : - Implicit 第二种方式:-隐式

startActivity(new Intent("dk.westsoftdevelopment.elaegen.OpenTokActivity"));

Hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM