从<a href=""> link in WebView works with API 19 but not with API 17

时间:2015-09-22 15:21:31

标签: android android-intent android-webview android-4.4-kitkat android-4.2-jelly-bean

I am trying tu use <a href="intent:#Intent;..."> links from web pages embedded in the APK of my application to interact with it. These pages are displayed with a WebView.

This works fine with my API 19 device and with my API 19 emulator, but not with my API 17 emulators. These ones make the WebView to display the dreaded "WEBPAGE NOT AVAILABLE" message instead of lauching the Intent embedded in the a link:

enter image description here

获取意图

我无法确定API 19和API 17之间行为不同的原因:

  • 这与API版本有关吗? (但是一些看起来比API 19旧的SO帖子引用了这样的Intent链接)
  • 这是否有效&#34;意外&#34;在我的API 19设备上(不正确的Intent HTML链接或<intent-filter>?我做过的研究表明这是有效的)
  • 或者我错过了什么,也许是在权限中?

所涉及的代码如下:

清单

[...]

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

[...]

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MyAppMain"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <!-- "THIS FILTER IS THE RELEVANT ONE: IT CATCHES INTENTS SENT FROM THE WEBVIEW" -->
        <intent-filter>
            <action android:name="com.example.myapp.CALL_FROM_LINK" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

    </activity>

 [...]

</application>

参与活动

public class MyAppMain extends Activity { 
    [...]

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        // The actual UI is HTML code displayed in a WebView
        final WebView webview = new WebView(this);

        try {
            // Determines the page to load. "page1.html" by default if no extra available.
            final Bundle extras = getIntent().getExtras();

            final String asset = extras != null ? extras.getString("page") : "page1.html";
            final InputStream is = getAssets().open(asset, AssetManager.ACCESS_BUFFER);

            // The line below does just put the content of the stream in a String
            final String html = StreamHelper.InputStreamToString(is); 

            // Loads the page into the WebView.          
            webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);

        } catch (IOException e) {
            throw new RuntimeException(e); // For now :)
        }  

        setContentView(webview); 
    } 

    [...]
}

推出Intent 的HTML链接:

<a href="intent:#Intent;action=com.example.myapp.CALL_FROM_LINK;S.page=page2.html;end">Other content</a>

0 个答案:

没有答案