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

发布于 2024-11-07 09:20:22 字数 3334 浏览 3 评论 0原文

我用 diff 创建了一个列表视图。每个项目的活动。当用户单击“打卡”时,我想获取当前时间/日期并以尽可能快的方式将该数据发送到网络服务器(无需经过两步过程来确认)。这将用于第二个活动类。

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

客户.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");
    }
}

I created a listview with diff. activity to each items. When the user click on "clock in" I would like to grab the current time/date and send that data to the webserver in quickest way possible (without going through 2 step process to confirm). This will be for secondActivity class.

UPDATE* I am planning to add a password to the time/date within the phone so the user cant change them. I prefer current time/date within the phone instead of server time because if theres no signal/reception theres no way to clock in. How can I be able to grab the current time/date within the phone?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花落人断肠 2024-11-14 09:20:22

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

看一下代码:

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'] 变量。

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

the quickest way would be creating a new thread and opening a connection to the server.

Take a look to the code:

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

Then on your server-side you've to read the $_GET['data'] variable if you're working with php.

Please consider that this solution is not ok for different timezones. I would probably rely on server-side date.

月依秋水 2024-11-14 09:20:22

如何使用时间:

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

How about using Time:

    Time timeToday = new Time();
    timeToday.setToNow();
    today = timeToday.year+"-"+ timeToday.MONTH+"-"+timeToday.monthDay;
乖乖公主 2024-11-14 09:20:22

为什么依赖用户设备的时间?如果我更改手机上的时间然后打卡会怎样?您将如何处理不同的时区?

为什么不简单地依赖网络服务器的服务器时间,因为你知道你可以依赖它并且你已经在调用网络服务器了?

Why rely on the time from a users device? What if I changed the time on my handset then clocked in? How are you going to handle different timezones?

Why not simply rely on server time of the webserver since you know you can depend on this and you're already making a call to the webserver?

儭儭莪哋寶赑 2024-11-14 09:20:22

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

    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 等网络库将其发送到服务器。

use this to get current date and time:

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

And call this function when user clicks to button and send this to server using networking library like Volley or Retrofit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文