我制作了一个1.0g的phonegap应用程序,它通过Google Map加载当前位置。我还创建了一个插件,用于在屏幕顶部显示活动指示器,即在iPhone的状态栏上。
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代码如下:
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代码如下:
#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启动/停止?
答案 0 :(得分:1)
你可以使用notificationEx而不是自己做,但这对知识有益:)
无论如何,地图是基于ajax的吗?如果是,并且您正在使用jQuery,请分配.ajaxStart()
和.ajaxStop()
个功能。
我执行以下操作:
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,请使用以下内容:
function onDeviceReady() {
/* ===============
// Bind Ajax calls with
// activity Indicator - ZEPTO
// =============== */
$(document).on('ajaxStart',function() {
navigator.notificationEx.activityStart();
}).on('ajaxStop',function() {
navigator.notificationEx.activityStop();
});
//other stuff
}