0

我想知道如何设置我的应用程序以每隔 15 分钟通过 API 发布更新其详细信息。现在,我知道如何使用 get 和使用线程,以便在访问 API 时为其创建加载器。

这是我的做法:

private int authenticateLogin()
  {
      EditText user = ((EditText)findViewById(R.id.username));
      EditText pass = ((EditText)findViewById(R.id.password));

      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

      StrictMode.setThreadPolicy(policy); 

      String username = user.getText().toString(), password = pass.getText().toString();

      String URL = "MyUrl";
      String authData = "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP);
      HttpClient httpclient = new DefaultHttpClient();
      HttpGet httpget = new HttpGet(URL);
      httpget.setHeader("Authorization", authData); 
      HttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
            StatusLine sl = response.getStatusLine();
            int statCode = sl.getStatusCode();
            if (statCode == 200) {
                String entityStringDrivers = EntityUtils.toString(response.getEntity());
                Intent i = new Intent(Login.this,DriverLogin.class);
                i.putExtra("stringDrivers", entityStringDrivers);
                startActivity(i);
                return 100;
            }
            else
            {
                user.setText("");
                pass.setText("");
                Toast.makeText(getBaseContext(), "Unauthorized Login", Toast.LENGTH_SHORT).show(); 
                return 100;
            }
        } catch (Exception e) {
            // Auto-generated catch block
            e.printStackTrace();
            return 100;
        }
        finally {
        }

  }

我想知道在发帖时我应该怎么做并在后台做。我不知道从哪里开始特别是每 15 分钟的 POST。有任何想法吗?谢谢!

4

4 回答 4

0

如果你想做这个背景,创建一个服务并在你的主要活动的 onCreate 或一些 BroadcastReceiver 中启动它

例如:(您需要添加 try-catch 或其他类型的保护代码)

public class MyService extends Service {

    @Override
    public void onStartCommand(Intent intent, int flags, int startId) {
        //Use intent to pass your username and password here
        //start a PostThread here.      
    }

    private class PostThread extends Thread {
        private static final int INTERVAL = 15 * 60 * 1000L;
        private boolean canceled = false;
        @Override
        public void run() {
            while (!canceled) {
                Post();
                Thread.sleep(INTERVAL); // or you can use a Timer to trigger this 
            }            
        }
        public void interrupt() { canceled = true; super.interrupt();} 
    }
}
于 2013-09-03T02:57:49.647 回答
0

您可以检查一下,并使用 Intent Services 在后台运行。

android timer,对于意图服务,请查看Intent Services

于 2013-09-03T02:50:40.263 回答
0

使用计时器的处理程序如下:
在片段调用startTimeReqTask()中启动计时器。

private Handler m_handler = new Handler();
....

   Runnable m_handlerTask = new Runnable() {

        @Override
        public void run() {
            m_handler.postDelayed(m_handlerTask, 900000); // 15 minutes
            new authenticateLoginTask().execute(); // POST (your asynctask)
        }
    };


    private void startTimeReqTask() // start timer
    {
        m_handlerTask.run(); 
    }

    private void stopTimeReqTask() // stop time
    {
        m_handler.removeCallbacks(m_handlerTask);
    }

并使用AsyncTaskauthenticateLoginTask 在后台进行。

于 2013-09-03T02:53:51.697 回答
0
 Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // call your api  method here
                }
            });
        }
    }, 0, /*here define your internal*/);

// when you no longer required this api call just call "cancel" method of timer
于 2013-09-03T03:36:00.917 回答