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:
我无法确定API 19和API 17之间行为不同的原因:
Intent
链接)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>