Android:当用户单击该项目时,如何将当前时间/日期直接发送到网络服务器
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最快的方法是创建一个新线程并打开与服务器的连接。
看一下代码:
然后,如果您使用 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:
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.
如何使用时间:
How about using Time:
为什么依赖用户设备的时间?如果我更改手机上的时间然后打卡会怎样?您将如何处理不同的时区?
为什么不简单地依赖网络服务器的服务器时间,因为你知道你可以依赖它并且你已经在调用网络服务器了?
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?
使用它来获取当前日期和时间:
并在用户单击按钮时调用此函数,并使用 Volley 或 Retrofit 等网络库将其发送到服务器。
use this to get current date and time:
And call this function when user clicks to button and send this to server using networking library like Volley or Retrofit.