Android URL 方案 : intent. getData() == null (PhoneGap)

标签 android cordova android-intent url-scheme

所以我试图获取一个被调用到我的应用程序中的 URL,但在它打开后,它似乎总是 null不管我做什么。

这是我的 list 的相关部分:

<activity android:name="myapp" android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
            android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

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

    </activity>

这是我的java:

@Override
public void onStart() {

    super.onStart();
    final Intent intent = getIntent();

    Uri url = intent.getData();

    Log.e("myLog", "Url = " + url);

}

日志正在输出“Intent = null”,我没有错误。

我不是 Java 开发人员,所以我可能遇到一个我不认识的简单问题。我将使用 sendJavascript 将其传递到我的 PhoneGap 应用程序中但作为 PhoneGap 应用程序似乎与此问题无关。

编辑:

我似乎只得到第一个 <intent-filter>当我登录 intent .这是日志:

03-13 12:36:18.593: E/MyAppLog(13793): Intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.wd.myapp/.myapp (has extras) }

解决方案:

在阅读了数十篇关于如何实现 URL 方案以打开您的 PhoneGap 应用程序的帖子后,没有一篇非常健壮或根本无法正常工作,我将发布我的完整解决方案并希望有人觉得它有用。

第一步:AndroidManifest.xml - 添加 <intent-filter>给你的<activity> ,将“urlScheme”更改为您喜欢的任何内容。在这种情况下,您可以单击指向 urlScheme:///?key=val 的链接,它会打开应用程序。

<intent-filter>
    <data android:scheme="urlScheme" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

第二步:myApp.java - 在 onCreate 下面添加以下内容.如果您使用 singleTask , 请务必将您的 intent onStart 中的逻辑功能(而不是在 onCreate 中,正如其他人所鼓吹的那样),因为这将允许您链接到您的应用程序,无论它是否已经在运行。

@Override
public void onStart() {

    super.onStart();
    final Intent intent = getIntent();

    String url = intent.getDataString();

    if(url != "" && url != null) {
        super.sendJavascript("setTimeout(function() { handleOpenURL('" + url + "'); },1);");
    }                


}

@Override
protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent); setIntent(intent);
}

有关 onNewIntent 的解释,请参阅已接受的答案覆盖。

最佳答案

尝试覆盖 onNewIntent。问题是 getIntent 返回启动 ActivityIntent,但不是最近的一个。但是您可以使用 setIntent 方法覆盖它。

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    setIntent(intent); 
}

关于Android URL 方案 : intent. getData() == null (PhoneGap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15390296/

相关文章:

android - 如何让 init.d 中的脚本在 Android 启动时执行?

android - 如何通过权限为 "com.android.externalstorage.documents"的uri获取文件路径

android - 使用静态库创建 PhoneGap 插件

javascript - 处理 iOS 虚拟键盘 “previous” , “next” , “Go/Enter” 按钮

android - 将 TelecomManager 与我们的自定义协议(protocol)结合使用

android - 重新连接后无法再写入 BLE 设备的特性

Cordova Hook 顺序

android - 动态注册时未调用 BroadcastReceiver onReceive()

java - json extra 在其他 Intent 上为 null

java - 通过 Intent 传递对象的 ArrayList - Java (Android)