如何使用Android自定义URL架构的意图?

时间:2013-03-14 11:34:26

标签: android cordova android-activity custom-url

基本上我有How to implement my very own URI scheme on Android

中描述的相同问题

我按照已回答问题的说明添加到我的Android清单中:

<activity android:name=".MyUriActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="myapp" android:host="path" />
    </intent-filter>
</activity>

另外,我添加了一个像这样的新活动:

import android.app.Activity;
import android.os.Bundle;

public class MyUriActivity extends Activity {
    @Override
    public void onCreate(Bundle b){
        System.out.println("URI CLICKED!");
    }
}

我的Android应用程序基于Phonegap,所以我只是添加了一个指向我的index.html的链接:

<a href="myapp://foo">Click me</a>

当我点击时,我在控制台中看到以下错误:

03-14 12:23:16.177: E/Cordova(629): Error loading url myapp://foo
03-14 12:23:16.177: E/Cordova(629): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=myapp://foo }
03-14 12:23:16.177: E/Cordova(629):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
03-14 12:23:16.177: E/Cordova(629):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
03-14 12:23:16.177: E/Cordova(629):     at android.app.Activity.startActivityForResult(Activity.java:3351)
03-14 12:23:16.177: E/Cordova(629):     at android.app.Activity.startActivityForResult(Activity.java:3312)
03-14 12:23:16.177: E/Cordova(629):     at android.app.Activity.startActivity(Activity.java:3522)
03-14 12:23:16.177: E/Cordova(629):     at android.app.Activity.startActivity(Activity.java:3490)
03-14 12:23:16.177: E/Cordova(629):     at org.apache.cordova.CordovaWebViewClient.shouldOverrideUrlLoading(CordovaWebViewClient.java:209)
03-14 12:23:16.177: E/Cordova(629):     at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:274)
03-14 12:23:16.177: E/Cordova(629):     at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:376)
03-14 12:23:16.177: E/Cordova(629):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 12:23:16.177: E/Cordova(629):     at android.os.Looper.loop(Looper.java:137)
03-14 12:23:16.177: E/Cordova(629):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-14 12:23:16.177: E/Cordova(629):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 12:23:16.177: E/Cordova(629):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 12:23:16.177: E/Cordova(629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-14 12:23:16.177: E/Cordova(629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 12:23:16.177: E/Cordova(629):     at dalvik.system.NativeStart.main(Native Method)

所以我似乎错过了活动。怎么了?

1 个答案:

答案 0 :(得分:2)

  1. 您没有致电super.onCreate(b);
  2. 网址myapp://foo与您的IntentFilter不符。它应为myapp://path/,或者您可以更改IntentFilter并删除host部分。

    <data android:scheme="myapp"/>