---
title: "[源代码]一个有用的链接管理器"
author: "haiker"
date: "2011-02-23T11:25:48Z"
tags: ["Palm"]
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-02-23T11:25:48Z"
id: 919150
url: "https://haiker.iteye.com/blog/919150"
---

```
var ConnectMgr = Class.create({
    initialize:function() {
        this.connected = 0;
        this._checkConnection();
    },

    _checkConnection : function() {
        try {
            var controller = Mojo.Controller.stageController.activeScene();
            controller.serviceRequest('palm://com.palm.connectionmanager/',
              {
                  method: 'getstatus',
                  parameters:
                    {
                        subscribe: true
                    },
                  onSuccess: this._checkSuccess.bind(this),
                  onFailure: this._checkFailure.bind(this)
              });
        }
        catch (e) {
            Mojo.Log.error('ConnectMgr -', e);
        }
    },

    _checkSuccess:function(response) {
        Mojo.Log.info('ConnectMgr - _checkSuccess', response.isInternetConnectionAvailable);
        this.connected = response.isInternetConnectionAvailable;
    },

    _checkFailure:function(response) {
        Mojo.Log.error('ConnectMgr - Connection Check Error:', response.errorText, '(Code', response.code, ')');
    }
});
```

 

 使用方式：

```
activate : function(event) {
    // Check connection status
    connmgr = new ConnectMgr();
}
```

 

```
if (!connmgr.connected) {
    Mojo.Controller.errorDialog('Request cannot be processed without a network connection.');
    return;
} 
```