Android:如何在用户点击项目时将当前时间/日期直接发送到网络服务器

时间:2011-05-16 15:49:02

标签: android datetime listview android-activity clock

我用diff创建了一个listview。对每个项目的活动。当用户点击“时钟输入”时,我想获取当前时间/日期,并以最快的方式将数据发送到网络服务器(无需经过两步确认)。这将是secondActivity类。

更新*我打算在手机内的时间/日期添加密码,以免用户更改密码。我更喜欢手机中的当前时间/日期而不是服务器时间,因为如果没有信号/接收,则无法进入。我怎样才能获取手机中的当前时间/日期?

Customer.java

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Customer extends ListActivity
{
    TextView selection;
    String[] items = { "Start Trip", "Clock in", "Customer Svc", 
            "Independent Inspection", "Pick Up", "Log Out" };

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, items));
        selection = (TextView) findViewById(R.id.selection);
    }

private static final int ACTIVITY_0 = 0;
private static final int ACTIVITY_1 = 1;
private static final int ACTIVITY_2 = 2;

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);
    final Intent intent = new Intent();
    // Set up different intents based on the item clicked: 
    switch (position)
    {
        case ACTIVITY_0:
            intent.setClass(this, com.company.merrill.IntentIntegrator.class);
            break;
        case ACTIVITY_1:
            intent.setClass(this, com.company.merrill.SecondActivity.class);
            break;
        case ACTIVITY_2:
            intent.setClass(this, com.company.merrill.ThirdActivity.class);
            break;
        default:
            break;
    }
    // Pass the item position as the requestCode parameter, so on the `Activity`'s
    // return you can examine it, and determine which activity were you in prior. 
    startActivityForResult(intent, position);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == RESULT_OK)
    {
        // Perform different actions based on from which activity is
        // the application returning:
        switch (requestCode)
        {
            case ACTIVITY_0:
                // contents contains whatever the code was
                String contents = intent.getStringExtra("SCAN_RESULT");

                // Format contains the type of code i.e. UPC, EAN, QRCode etc...
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

                // Handle successful scan. In this example 
                // I just put the results into the TextView
                resultsTxt.setText(format + "\n" + contents);
                break;
            case ACTIVITY_1:
                // TODO: handle the return of the SecondActivity
                break;
            case ACTIVITY_2:
                // TODO: handle the return of the ThirdActivity
                break;
            default:
                break;
        }
    }
    else if (resultCode == RESULT_CANCELED)
    {
        // Handle cancel. If the user presses 'back' 
        // before a code is scanned.
        resultsTxt.setText("Canceled");
    }
}

4 个答案:

答案 0 :(得分:0)

如何使用时间:

    Time timeToday = new Time();
    timeToday.setToNow();
    today = timeToday.year+"-"+ timeToday.MONTH+"-"+timeToday.monthDay;

答案 1 :(得分:0)

为什么要依赖用户设备的时间?如果我改变手机上的时间然后输入怎么办?你打算如何处理不同的时区?

为什么不依靠网络服务器的服务器时间,因为你知道你可以依赖这个并且你已经打电话给网络服务器了?

答案 2 :(得分:0)

最快的方法是创建一个新线程并打开与服务器的连接。

看看代码:

new Thread(new Runnable() {

    public void run() {

        URL url = new URL("http://www.example.com/?data="+System.currentTimeMillis());

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        //if there is no need to read the content then we close the connection

        urlConnection.disconnect();
    }
}).start();

然后在服务器端,如果你正在使用php,你将阅读$ _GET ['data']变量。

请考虑此解决方案不适用于不同的时区。我可能会依赖服务器端日期。

答案 3 :(得分:0)

使用它来获取当前日期和时间:

    private String getDateandTime() {
       Calendar c = Calendar.getInstance();
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String formattedDate = df.format(c.getTime());
       Log.e("Activity name", "time date "+formattedDate);
       return formattedDate;
   }

当用户点击按钮并使用Volley或Retrofit等网络库将其发送到服务器时调用此函数。