首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Phonegap -检测网络活动

Phonegap -检测网络活动
EN

Stack Overflow用户
提问于 2011-11-03 18:07:13
回答 1查看 2.1K关注 0票数 1

场景

我做了一个phonegap 1.0.0应用程序,通过谷歌地图加载当前位置。我还创建了一个插件,用于在屏幕顶部显示活动指示器,即在iPhone的状态栏上。

代码语言:javascript
运行
复制
function onBodyLoad()
{   
    document.addEventListener("deviceready",onDeviceReady,false);
};

function onDeviceReady()
{
    //Get the plugin object
    var ai = window.plugins.ActivityIndicator;
    //Stop the spin, if any activity was going on earlier
    ai.end();
    if(navigator.geolocation) {
        //Start spinning, indicating that some network activity is going on
        ai.set();
        navigator.geolocation.getCurrentPosition(initSearch);
    }
    else alert("Please try reloading");
    //Stop spinning after 10 seconds
    window.setTimeout(ai.end, 10000);
};

插件的Javascript代码如下:

代码语言:javascript
运行
复制
var ActivityIndicator = function() {};
ActivityIndicator.prototype.set = function() 
{
    PhoneGap.exec("ActivityIndicator.start");
};
ActivityIndicator.prototype.end = function() 
{
    PhoneGap.exec("ActivityIndicator.end");
};
ActivityIndicator.install = function() {
    if(!window.plugins)
        window.plugins = {};
    window.plugins.ActivityIndicator = new ActivityIndicator();
    return window.plugins.ActivityIndicator;
};
PhoneGap.addConstructor(ActivityIndicator.install);

插件的Objective-C代码如下:

代码语言:javascript
运行
复制
#import <Foundation/Foundation.h>

#ifdef PHONEGAP_FRAMEWORK
#import <PhoneGap/PGPlugin.h>
#else
#import "PGPlugin.h"
#endif

@interface ActivityIndicator : PGPlugin {}
-(void) start:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
-(void) end:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end

@implementation ActivityIndicator

- (void)start:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;
}

- (void)end:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
}

@end

问题

正如您在上面的代码片段中所看到的,我已经将旋转的超时设置为10秒后停止。

问题

当有网络活动时,如何使activityIndicator启动/停止?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-08 01:55:19

您可以使用notificationEx而不是您自己的,但它对您的知识有好处:)

不管怎样,map调用是基于ajax的吗?如果是,并且您正在使用jQuery,请分配.ajaxStart().ajaxStop()函数。

我执行以下操作:

代码语言:javascript
运行
复制
function onDeviceReady() {
    /* ===============
    // Bind Ajax calls with
    // activity Indicator
    // =============== */
    $(document).ajaxStart(function() {
        navigator.notificationEx.activityStart();
    }).ajaxStop(function() {
        navigator.notificationEx.activityStop();
    });
//other stuff
}

这样,所有ajax都将在发生时显示在活动中。

更新:对于Zeptojs -在0.8版或更高级的ajax global implemented only on v0.8上,使用以下命令:

代码语言:javascript
运行
复制
function onDeviceReady() {
    /* ===============
    // Bind Ajax calls with
    // activity Indicator - ZEPTO
    // =============== */
    $(document).on('ajaxStart',function() {
        navigator.notificationEx.activityStart();
    }).on('ajaxStop',function() {
        navigator.notificationEx.activityStop();
    });
//other stuff
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7993083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档