【发布时间】:2013-03-13 15:59:24
【问题描述】:
所以我试图获取一个被调用到我的应用程序中的 URL,但在它打开后,无论我做什么,它似乎总是null。
这是我清单的相关部分:
<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 时,我似乎只得到第一个<intent-filter>。这是日志:
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 覆盖的解释,请参阅接受的答案。
【问题讨论】:
-
我认为最终意图意图 = getIntent();应该是最终的 Intent intent = intent.getIntent();
-
怎么样? “意图”还不存在。不过我会试试看的。
-
是的,这不是一种方法。我相信我按原样登录了
intent并从中得到了一些东西。它只是在其上调用方法以从中获取数据始终为空。 -
尝试覆盖
onNewIntent。@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } -
@dg988 你的问题解决了吗?
标签: android cordova android-intent url-scheme