Android:每隔发送一次数据并发送到Web服务器时,保存gps位置
在我的mainActivity中,我有一个列表视图,用户可以在其中选择一个。作为示例,mainActivity中有3个列表,分别是startTripActivity、ClockinActivity和CustomerSvcActivity 对于starttripactivity,用户将单击该按钮,它将显示按钮,单击后,它将显示其位置的toast消息信息并发送到服务器。对于ClockInactive,用户可以单击以显示当前时间/日期,然后单击按钮将数据发送到服务器 对于customersvcActivity,用户将单击该按钮,它将打开条形码扫描仪并返回结果并将其发送到服务器。我想做的是发送到服务器的每个活动,我还想发送带有数据的gps位置。例如,starttrip将向服务器发送gps信息,clockin将向服务器发送clockin时间/日期加上他们的gps位置信息,条形码结果数据加上他们扫描发送到服务器的代码的gps位置信息 我想看看一些样品和建议。我找到的所有搜索大多是如何每xx分钟获取gps数据,这并不完全是我想要的,很少有其他搜索对我帮助不大。这篇文章的最底层是我与Web服务器通信的示例代码 谢谢 MainActivity.javaAndroid:每隔发送一次数据并发送到Web服务器时,保存gps位置,android,gps,webserver,location,latitude-longitude,Android,Gps,Webserver,Location,Latitude Longitude,在我的mainActivity中,我有一个列表视图,用户可以在其中选择一个。作为示例,mainActivity中有3个列表,分别是startTripActivity、ClockinActivity和CustomerSvcActivity 对于starttripactivity,用户将单击该按钮,它将显示按钮,单击后,它将显示其位置的toast消息信息并发送到服务器。对于ClockInactive,用户可以单击以显示当前时间/日期,然后单击按钮将数据发送到服务器 对于customersvcActi
public class Customer extends ListActivity
{
CustomerListItem[] items = {
new CustomerListItem("Start Trip", StartTripActivity.class),
new CustomerListItem("Clock in", ClockinActivity.class),
new CustomerListItem("Customer Svc", CustomerSvcActivity.class),
private String username;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.customer);
setListAdapter(new ArrayAdapter<CustomerListItem>(
this, android.R.layout.simple_list_item_1, items));
selection = (TextView) findViewById(R.id.selection);
resultsTxt = (TextView) findViewById(R.id.cancel);
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this, items[position].getActivity());
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 0:
// TODO: handle the return of the StartTripActivity
break;
.............
}
ClockinActivity.java
public class ClockinActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clockin);
TextView textView= (TextView)findViewById(R.id.Date);
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
textView.setText(currentDateTimeString);
Thread myThread = null;
Runnable runnable = new CountDownRunner();
myThread= new Thread(runnable);
myThread.start();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Pop("Back Button");
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void Pop(String string) {
// TODO Auto-generated method stub
}
public void doWork() {
runOnUiThread(new Runnable() {
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":"+ minutes + ":"+ seconds;
txtCurrentTime.setText(curTime);
}catch (Exception e) {
}
}
});
}
class CountDownRunner implements Runnable{
// @Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
doWork();
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}catch(Exception e){
}
}
Button btn = (Button) findViewById(R.id.btn_OK);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);
//finish();
}
CustomerSvcActivity.java
public class CustomerSvcActivity extends Activity {
private Button btnscan;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
this.startActivity(new Intent(CustomerSvcActivity.this,Customer.class));
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customersvc);
btnscan = (Button) findViewById(R.id.scanbtn);
btnscan.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
IntentIntegrator.initiateScan(CustomerSvcActivity.this);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String upc = scanResult.getContents();
// need to return the result without display and send to server???
}
}
break;
发送数据的示例类
public class Sample extends Activity {
/** Called when the activity is first created. */
TextView result;
EditText text;
Button send;
private ArrayList<NameValuePair> postParameters;
AlertDialog.Builder alert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alert = new AlertDialog.Builder(this);
alert.setCancelable(true);
result=(TextView)findViewById(R.id.result);
text=(EditText)findViewById(R.id.text);
send=(Button)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//postParameters.add(new BasicNameValuePair("text",
//text.getText().toString()));
postData(text.getText().toString());
//saveData();
showData();
}
public void showData()
{
try {
String response = CustomHttpClient
.executeHttpGet("http://www.merrill.com/android.php");
result.setText(response);
} catch (Exception e) {
}
}
公共类示例扩展活动{
/**在首次创建活动时调用*/
文本视图结果;
编辑文本;
按钮发送;
私有ArrayList后参数;
AlertDialog.Builder警报;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alert=新建AlertDialog.Builder(此);
alert.setCancelable(真);
结果=(TextView)findViewById(R.id.result);
text=(EditText)findViewById(R.id.text);
send=(按钮)findviewbyd(R.id.send);
send.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
//添加(新的BasicNameValuePair(“文本”),
//text.getText().toString());
postData(text.getText().toString());
//saveData();
showData();
}
公共void showData()
{
试一试{
字符串响应=CustomHttpClient
.executeHttpGet(“http://www.merrill.com/android.php");
result.setText(应答);
}捕获(例外e){
}
}
Android的位置API只是周期性的。如果您想要由用户的任何操作触发的一次性位置修复,您可以使用LocationManager.getLastKnownLocation(),但您需要检查返回的修复的时间戳。要获得新的修复,您真正需要做的是:
-request location data using a small period time
-wait until the first location fix comes in
-cancel your location request
这将需要异步通信,您可以使用自己最熟悉的方式进行通信。如果您可以访问易于包含的系统,则可以使用回调函数或传入可运行或消息传递系统。GPS可能需要几分钟才能返回,并且可能无法获得有效的修复,因此您必须这是我的荣幸
希望这有帮助。@拖船,正如您在我的startTripActivity中看到的。我可以单击按钮并立即请求gps信息。我希望在其他活动中包含该按钮,但只需单击一个按钮(以及其他数据)。这可能吗?在StartTripActivity中,在活动处于活动状态时,您似乎一直在请求GPS更新。如果您在其他活动中执行此操作,其工作方式也会相同。根据用户的不同,此方法可能会有问题。例如,如果他们在出现修复之前按下按钮。您将怎么办不过,ing是一种快速而简单的方式来获取您想要的东西。@拖船们,我是否必须将相同的StartTripactivity代码添加到其他所有活动中?是否有一种方法可以使其他活动更容易地使用该代码,以及如何获取该数据并将其发送到Web服务器?如果您不想到处剪切和粘贴GPS代码,您可以将其推到基本动作中ivity类继承自或您可以创建一个特殊的GPS类,该类将执行所有GPS逻辑,并且每个活动都将拥有一个作为组件的GPS类。@tugs您会推荐哪种?我知道每个人都有不同的风格和偏好,但我想知道您的意见?
-request location data using a small period time
-wait until the first location fix comes in
-cancel your location request