English 中文(简体)
在一段时期后,多次要求网络服务
原标题:calling webservice repeatedly after a period of time

can any one please tell me how to call a webservice repeatedly for every period of time.(for exapmple i want to call webservice for every 5min). In my app I have a spinner that allows user to select after how many minutes the webservice has to be refreshed. here is the code i wrote using countdown timer.

我在此写道,如果选择了“不复健”的钥匙,应当停止时间。 一旦选择了除第一项目以外的任何项目,然后如果我选择第一个项目(即:不复读),时间就不再停止。 在此,我呼吁采用定点方法,一再呼吁。

 private String[] refreshtimes = { "do not refresh","1 minute Refresh",  "5minute           Refresh",
        "15 minute Refresh", "30 minute Refresh", "45 min Refresh",
        "60 minute Refresh" };

sp_refresh = (Spinner) findViewById(R.id.refresh);
ArrayAdapter<String> spdptr = new ArrayAdapter<String>(
            getApplicationContext(),    android.R.layout.simple_spinner_item,
            refreshtimes);
    sp_refresh.setAdapter(spdptr);

    sp_refresh.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int position, long id) {

            if(position!=0 )
            {
                int time=0;

            switch (position) {
            case 1:                 
                time=1;
                break;
            case 2:                 
                time=5;
                break;                  
            case 3:                 
                time=15;
                break;
            case 4:                 
                time=30;
                break;
            case 5:                 
                time=45;
                break;
            case 6:                 
                time=60;
                break;
            default:
                break;
            }
            counter = new MyCount(time*1000,1000);
            counter.start();
            }
            else if(position==0&&counter!=null)
            {   
                counter.cancel();
                counter=null;
                Toast.makeText(getApplicationContext(), "u r in    elsee",10000).show();
            }

        }
 public class MyCount extends CountDownTimer{

    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
   // tv.setText("done!");
        callservice();
        Toast.makeText(getApplicationContext(), "hi..",10000).show();
        //onCreate(savedInstanceState);

        this.start();
    }

    @Override
    public void onTick(long millisUntilFinished) {
   // tv.setText("”Left: " + millisUntilFinished/1000);
        Toast.makeText(getApplicationContext(), "Left: "       +                                 millisUntilFinished/1000,10000).show();
    }

    }
最佳回答

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/timerTask.html

另见:timer,其中利用时台来安排任务。

问题回答

低于法典

    package com.Test_dia;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class Test_diaActivity extends Activity {

    String[] refreshtimes = { "do not refresh", "1 minute Refresh",
            "5minute           Refresh", "15 minute Refresh",
            "30 minute Refresh", "45 min Refresh", "60 minute Refresh" };
    private Spinner Spi;
    private int time;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spi = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> spdptr = new ArrayAdapter<String>(
                getApplicationContext(), android.R.layout.simple_spinner_item,
                refreshtimes);
        Spi.setAdapter(spdptr);

        Spi.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                if (position > 0) {

                    switch (position) {
                    case 1:
                        time = 1;
                        break;
                    case 2:
                        time = 5;
                        break;
                    case 3:
                        time = 15;
                        break;
                    case 4:
                        time = 30;
                        break;
                    case 5:
                        time = 45;
                        break;
                    case 6:
                        time = 60;
                        break;
                    default:
                        break;
                    }

                    call_timmer(time);

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    private void call_timmer(int i) {
        // TODO Auto-generated method stub

        int t = i * 60000;

        Timer myt = new Timer();
        myt.schedule(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                timerMethod();
            }
        }, 0, 60000);
    }

    private void timerMethod() {
        this.runOnUiThread(doSomething);
    }

    private Runnable doSomething = new Runnable() {
        public void run() {
            // Your code for doing something
            Toast.makeText(Test_diaActivity.this, "dhaval Sodha Parmar",
                    Toast.LENGTH_LONG).show();
        }
    };
}

如果是正确的,它就正确。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签