在网上找了很久,都是在前台起作用的,只有自己实现了,还有些小问题,但基本实现了。放在附件里了,带有一个后台运行的例子。
以前写过一篇介绍NSInvocationOperation的文章:多线程之NSInvocationOperation,这篇文章是基于此的一个例子。
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { NSMutableArray *array; } @property (nonatomic, retain) NSMutableArray *array; @end
RootViewController.m
#import "RootViewController.h" @implementation RootViewController @synthesize array; - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Load" style:UIBarButtonItemStyleDone target:self action:@selector(loadData)]; NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:10000]; self.array = _array; [_array release]; } - (void) loadData { NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadDataWithOperation) object:nil]; [queue addOperation:operation]; [operation release]; } - (void) loadDataWithOperation { NSURL *dataURL = [NSURL URLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"]; NSArray *tmp_array = [NSArray arrayWithContentsOfURL:dataURL]; for(NSString *str in tmp_array) { [self.array addObject:str]; } [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; } #pragma mark Table view methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } [cell.textLabel setText:[self.array objectAtIndex:indexPath.row]]; return cell; } - (void)dealloc { [super dealloc]; [array release]; } @end
MQTT的官方推荐网站:
http://mqtt.org/software
使用IBM 的MQTT协议实现push消息
地址:
http://tokudu.com/2010/how-to-implement-push-notifications-for-android/
google code 下载MQTT moquette Broker 地址:
http://code.google.com/p/moquette-mqtt/
GIT 下载MQTT moquette client 地址:
https://github.com/fusesource/mqtt-client
参考链接
http://stackoverflow.com/questions/1243066/does-android-support-near-real-time-push-notification
http://stackoverflow.com/questions/1378671/push-notifications-in-android-platform
http://blog.boxedice.com/2010/10/07/android-push-notifications-tutorial/
http://dalelane.co.uk/blog/?p=1599
http://tokudu.com/2010/how-to-implement-push-notifications-for-android/
http://blog.isidorey.com/2011/05/cloud-to-device-and-back-with-android.html
http://www.hardill.me.uk/wordpress/?p=207
http://tokudu.com/demo/android-push/
附加一个好玩的链接
http://www.hardill.me.uk/wordpress/?p=204
http://mosquitto.org/
http://www.mengxiansheng.net/index.php/archives/216
上面有更多关于mqtt的介绍