我创建了一个列表视图,其中包含每个项目的差异活动。当用户单击"打卡"时,我想获取当前时间/日期并以最快的方式将该数据发送到网络服务器(无需经过 2 步过程进行确认)。这将用于第二活动类。
更新* 我打算在手机中的时间/日期添加密码,以便用户无法更改密码。我更喜欢手机中的当前时间/日期而不是服务器时间,因为如果没有信号/接收,就无法打卡。如何在手机中获取当前时间/日期?
客户.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");
}
}
最快的方法是创建一个新线程并打开与服务器的连接。
看看代码:
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'] 变量。
请注意,此解决方案不适用于不同的时区。我可能会依赖服务器端日期。
如何使用时间:
Time timeToday = new Time();
timeToday.setToNow();
today = timeToday.year+"-"+ timeToday.MONTH+"-"+timeToday.monthDay;
为什么要依赖用户设备的时间?如果我更改了手机上的时间然后打卡怎么办?您将如何处理不同的时区?
为什么不简单地依赖网络服务器的服务器时间,因为你知道你可以依赖它,并且你已经在调用网络服务器?
使用它来获取当前日期和时间:
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 等网络库将其发送到服务器。