问题描述:

I want to create an app in which i will be uploading a data to the server. Sense i am uploading a data to server so its not necessary that when user in moving the connection will not be constant some time while uploading data connection may lost. So what i want is if connection is lost something should run in background which will always serach for connection as soon as the connection is available it start sending the data to server.

Can any one tell me how to do this. Is there any way to find the strength of the signal of both wifi and mobile internet.

Any idea is appreciated.

Thanks in advance.

网友答案:

Using setInterval method, wrap your network connection state function(using phonegap api) inside it. So it will be trigger for specific time interval. Once the connection is establish, upload your data to the server

网友答案:

Let me start by trying to answer your first question: the background processing. In fact, there are multiple ways to achieve this. The first is a real background process, i.e. like a Linux daemon. Just lookup the various articles on that. Another one is to start a thread from the GUI thread. Here's a simple example:

public void onStartButtonClick(View view) {
    Log.i("onStartButtonClick", "clicked");
    mProgress = 0;
    new Thread() {
        @Override
        public void run() {
            Log.i("thread-" + Thread.currentThread().getId(), "run() method just started");
            while (mProgress < 100) {
                try {
                    Thread.sleep(mSleepTime);
                    mProgress++;
                    Log.i("thread-" + Thread.currentThread().getId(), "progress is " + mProgress);
                    mHandler.post(mUpdateGUI);
                } catch (InterruptedException e) {
                    // e.printStackTrace();
                }
            }
        }
    }.start();

Please notice that this is an over-simplified example. The mHandler.post() has been added as an option to call back the original GUI thread (it is not allowed to call the GUI directly from a thread). This requires some extra code in your class:

// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateGUI = new Runnable() {
    public void run() {
        updateProgress();
    }
};

The updateProgress method is the allowed to call on the GUI. Example:

private void updateProgress() {
    mProgressBar.setProgress(mProgress);
}

Hope this helps you in the right direction. Good luck.

相关阅读:
Top