zoukankan      html  css  js  c++  java
  • android 定时请求(两种实现方式)

    方式一: Handler + Runnable (借鉴网址:http://stackoverflow.com/questions/6207362/how-to-run-an-async-task-for-every-x-mins-in-android

        private final static int INTERVAL = 1000;  // 2 minutes
        private static final int ALL_COUNT = 5;    // 总资源数
        private TextView mTextView;
        private ProgressBar mProgressBar;
        
        private Handler mHandler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                mProgressBar.setProgress(msg.what);
            };
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mTextView = (TextView) this.findViewById(R.id.textView1);
            mProgressBar = (ProgressBar) this.findViewById(R.id.progressBar2);
            startRepeatingTask();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        Runnable m_handlerTask = new Runnable() {
            @Override
            public void run() {
                int count = doSomething();
                if (count <= ALL_COUNT) {
                    mHandler.sendEmptyMessage((int) ((count / (float) ALL_COUNT)*100));
                    mHandler.postDelayed(m_handlerTask, INTERVAL);
                } else {
                    stopRepeatingTask();
                }
            }
        };
    
        private int mCount = -1;
        /**
         * 此处调用 接口
         * @return
         */
        private int doSomething() {
            mCount = mCount + 1;
            return mCount;
        }
    
        void startRepeatingTask() {
            m_handlerTask.run();
        }
    
        /**
         * 此处 结束
    */ void stopRepeatingTask() { mTextView.setText("合成完成!"); mHandler.removeCallbacks(m_handlerTask); }

    方式二:AsyncTask

        private TextView mTextView;
        private ProgressBar mProgressBar;
        private SynthesisProgressTask mSynthesisProgressTask;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mTextView = (TextView) this.findViewById(R.id.textView1);
            mProgressBar = (ProgressBar) this.findViewById(R.id.progressBar2);
            mSynthesisProgressTask = new SynthesisProgressTask(mProgressBar, mTextView);
            mSynthesisProgressTask.execute("url");
        }
    
            @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
            private class SynthesisProgressTask extends AsyncTask<String, Integer, Integer> {
            private static final int ALL_MEDIA_COUNT = 10;
            private boolean isSynthesisFinished = false;
            private ProgressBar mInnerProgressBar;
            private TextView mInnerTextView;
    
            public SynthesisProgressTask(ProgressBar progressBar, TextView textView) {
                this.mInnerProgressBar = progressBar;
                this.mInnerTextView = textView;
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Integer doInBackground(String... params) {
                int i = -1;
                while (!isSynthesisFinished) {
                    synchronized (this) {
                        i = i + 1;
                        if (i == ALL_MEDIA_COUNT) {
                            isSynthesisFinished = true;
                        }
                        publishProgress((int) ((i / (float) ALL_MEDIA_COUNT) * 100));
                        try {
                            wait(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return i;
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                super.onPostExecute(result);
                mInnerTextView.setText("已完成!");
            }
    
            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                System.out.println("onSynth onProgress : " + values[0]);
                mInnerProgressBar.setProgress(values[0]);
                mInnerTextView.setText(values[0] + "%");
            }
    
        }
  • 相关阅读:
    MyBatis 配置文件 用户密码加密存储
    MyBatis 实例
    mybatis 入门搭建
    文件上传
    struts 结果类型
    Struts 拦截器
    log4j
    Struts Action 控制器
    Struts
    四、常用的Maven命令
  • 原文地址:https://www.cnblogs.com/nmj1986/p/3175129.html
Copyright © 2011-2022 走看看