插件回调上带有 document.location.href 的 ActivityNotFoundException

发布于 2024-12-26 06:06:26 字数 5125 浏览 2 评论 0原文

我的问题很难清楚地解释...

我调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-01-02 06:06:26

好吧...我终于找到了一个解决方案:

当我想要显示一个新页面时,我使用一个新插件通过 url 参数再次启动主要活动 DroidGap:

public class MyActivity extends DroidGap
{
  [...]

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    Intent i = getIntent();
    Bundle b = i.getExtras();

    if (b == null)
    {
        super.loadUrl(PROV_WWW + "html/fr/index.html");
    }
    else
    {
        String url = b.getString("URL");

        super.loadUrl(url);
    }
  }
}

这更像是一种解决方法,而不是真正的解决方案,但它确实有效。

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 :

public class MyActivity extends DroidGap
{
  [...]

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    Intent i = getIntent();
    Bundle b = i.getExtras();

    if (b == null)
    {
        super.loadUrl(PROV_WWW + "html/fr/index.html");
    }
    else
    {
        String url = b.getString("URL");

        super.loadUrl(url);
    }
  }
}

It's more a workaround than a real solution, but it's actually working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文