插件回调上带有 document.location.href 的 ActivityNotFoundException
我的问题很难清楚地解释...
我调用phonegap插件来下载并解压缩zip,并在回调中返回新页面的url以加载新页面:
bindings.js(绑定完成的地方) ) :
showGameDetailComplete: function(data) {
[...]
$('#play-game').bind('tap',function() {
var gameDownloader = new GameDownloader();
gameDownloader.downloadGame(data.url, data.id,
function(data) {
alert(data);
document.location.href = data;
}, function(data) {
alert('Download failed');
}
);
})
},
gamedownloader.js (JS - Android 之间的链接) :
GameDownloader.prototype.downloadGame = function(fileUrl, gameId,
successCallback, failureCallback) {
PhoneGap.exec(successCallback, failureCallback, "GameDownloader", "download", [ fileUrl, gameId]);
};
GameDownloaderPlugin.java (Phonegap Android 原生插件) :
public class GameDownloaderPlugin extends Plugin
{
[...]
@Override
public PluginResult execute(String action, JSONArray data, String callbackId)
{
result = null;
mCallbackId = callbackId;
if (action.equals(ACTION_DOWNLOAD))
{
try
{
downloadGame(data.getString(0), data.getString(1));
result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
return result;
}
catch (JSONException e)
{
result = new PluginResult(PluginResult.Status.ERROR, "Param errors");
}
}
else
{
result = new PluginResult(Status.INVALID_ACTION);
Log.d("DirectoryListPlugin", "Invalid action : " + action + " passed");
}
return result;
}
private void downloadGame(String fileUrl, String gameId)
{
try
{
/* Downloading the zip */
[...]
/* Unzip the zip */
Intent iUnzip = new Intent(ctx, GameUnzipActivity.class);
iUnzip.putExtra(UNZIP_GAMEID, gameId);
iUnzip.putExtra(UNZIP_SRCFILE, fileName);
ctx.startActivityForResult(this, iUnzip, GAME_UNZIP);
}
catch (IOException e)
{
if (LOG)
Log.d("GameDownloaderPlugin", "Error: " + e);
this.error(new PluginResult(PluginResult.Status.ERROR, "Error: " + e), mCallbackId);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case (GAME_UNZIP):
{
if (resultCode == Activity.RESULT_OK)
{
if (LOG)
Log.d("GameDownloaderPlugin", "Game unzipped! \\o/");
this.success(new PluginResult(PluginResult.Status.OK, "../../games/" + mGameId + "/"), this.mCallbackId);
}
else
{
if (LOG)
Log.d("GameDownloaderPlugin", "Bide!");
this.error(new PluginResult(PluginResult.Status.ERROR), this.mCallbackId);
}
}
break;
}
}
}
所以这里调用了 unzip 活动,然后在 onActivityResult() 中返回结果:
PluginResult(PluginResult.Status.OK, "../../games/" + mGameId + "/"), this.mCallbackId);
在这里...没问题,游戏解压良好,回调发送良好,在 bindings.js 中收到:
function(data) {
alert(data);
document.location.href = data + 'html/fr/index.html';
}
alert(data) 显示良好的回调,但 document.location.href 行 logcat 出现错误:
Error loading url content://com.tuto.android.provider.DataFileProvider/games/1/html/fr/index.html
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.tuto.android.provider.DataFileProvider/games/1/html/fr/index.html }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
at android.app.Activity.startActivityForResult(Activity.java:2817)
at com.phonegap.DroidGap.startActivityForResult(DroidGap.java:1523)
at android.app.Activity.startActivity(Activity.java:2923)
at com.phonegap.DroidGap$GapViewClient.shouldOverrideUrlLoading(DroidGap.java:1338)
at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
并提供信息,我使用提供程序来显示 /data/data/package/files 文件中包含的页面,这就是我有这个 URL 的原因:
content://com.tuto.android.provider.DataFileProvider/games/1/html/fr/index.html
如果您想要更多说明,请告诉我更多,这很难理解。
My problem is quite hard to explain clearly...
I call a phonegap plugin to download and unzip a zip, and on the callback i return the url of the new page to load the new page :
bindings.js (Where the binding is done) :
showGameDetailComplete: function(data) {
[...]
$('#play-game').bind('tap',function() {
var gameDownloader = new GameDownloader();
gameDownloader.downloadGame(data.url, data.id,
function(data) {
alert(data);
document.location.href = data;
}, function(data) {
alert('Download failed');
}
);
})
},
gamedownloader.js (The link between JS - Android) :
GameDownloader.prototype.downloadGame = function(fileUrl, gameId,
successCallback, failureCallback) {
PhoneGap.exec(successCallback, failureCallback, "GameDownloader", "download", [ fileUrl, gameId]);
};
GameDownloaderPlugin.java (The Phonegap Android native plugin) :
public class GameDownloaderPlugin extends Plugin
{
[...]
@Override
public PluginResult execute(String action, JSONArray data, String callbackId)
{
result = null;
mCallbackId = callbackId;
if (action.equals(ACTION_DOWNLOAD))
{
try
{
downloadGame(data.getString(0), data.getString(1));
result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
return result;
}
catch (JSONException e)
{
result = new PluginResult(PluginResult.Status.ERROR, "Param errors");
}
}
else
{
result = new PluginResult(Status.INVALID_ACTION);
Log.d("DirectoryListPlugin", "Invalid action : " + action + " passed");
}
return result;
}
private void downloadGame(String fileUrl, String gameId)
{
try
{
/* Downloading the zip */
[...]
/* Unzip the zip */
Intent iUnzip = new Intent(ctx, GameUnzipActivity.class);
iUnzip.putExtra(UNZIP_GAMEID, gameId);
iUnzip.putExtra(UNZIP_SRCFILE, fileName);
ctx.startActivityForResult(this, iUnzip, GAME_UNZIP);
}
catch (IOException e)
{
if (LOG)
Log.d("GameDownloaderPlugin", "Error: " + e);
this.error(new PluginResult(PluginResult.Status.ERROR, "Error: " + e), mCallbackId);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case (GAME_UNZIP):
{
if (resultCode == Activity.RESULT_OK)
{
if (LOG)
Log.d("GameDownloaderPlugin", "Game unzipped! \\o/");
this.success(new PluginResult(PluginResult.Status.OK, "../../games/" + mGameId + "/"), this.mCallbackId);
}
else
{
if (LOG)
Log.d("GameDownloaderPlugin", "Bide!");
this.error(new PluginResult(PluginResult.Status.ERROR), this.mCallbackId);
}
}
break;
}
}
}
So here the unzip activity is called, then return a result in onActivityResult() :
PluginResult(PluginResult.Status.OK, "../../games/" + mGameId + "/"), this.mCallbackId);
Here... No problem, the game is well unzipped, the callback is well sent, received in bindings.js :
function(data) {
alert(data);
document.location.href = data + 'html/fr/index.html';
}
The alert(data) show the good callback, but the document.location.href line logcat an error :
Error loading url content://com.tuto.android.provider.DataFileProvider/games/1/html/fr/index.html
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.tuto.android.provider.DataFileProvider/games/1/html/fr/index.html }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
at android.app.Activity.startActivityForResult(Activity.java:2817)
at com.phonegap.DroidGap.startActivityForResult(DroidGap.java:1523)
at android.app.Activity.startActivity(Activity.java:2923)
at com.phonegap.DroidGap$GapViewClient.shouldOverrideUrlLoading(DroidGap.java:1338)
at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
And for information, I use a provider to show my pages included in the /data/data/package/files file, that's why I have this URL :
content://com.tuto.android.provider.DataFileProvider/games/1/html/fr/index.html
If you want more explication tell me more, it's quite hard to understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧...我终于找到了一个解决方案:
当我想要显示一个新页面时,我使用一个新插件通过 url 参数再次启动主要活动 DroidGap:
这更像是一种解决方法,而不是真正的解决方案,但它确实有效。
Well... I've finally found a solution :
When I want a new page to be shown, I use a new plugin for starting the principal activity DroidGap a second time with the url parameter :
It's more a workaround than a real solution, but it's actually working.