0

谁能告诉我如何在每个时间段内重复调用网络服务。(例如,我想每 5 分钟调用一次网络服务)。在我的应用程序中,我有一个微调器,允许用户选择在多少分钟后必须刷新网络服务。这是我使用倒数计时器编写的代码。

这里我写了逻辑,使得选择旋转器中的“donot刷新”时,应停止计时器。一旦我选择了除第一项以外的任何项目,然后如果我选择了第一项(即不刷新),则计时器不会停止。这里我在 onfinish() 方法中调用 ws 以重复调用。

 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();
    }

    }
4

2 回答 2

2

试试下面的代码

    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();
        }
    };
}

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

于 2012-05-14T10:35:59.947 回答
0

您可以使用 java 类TimerTask来重复执行一个方法。

另请参阅:Timer,它使用 TimerTask 来安排任务。

于 2012-05-14T10:24:13.740 回答