LocationActivity

时间:2024-08-24 18:03:20
package com.baidu.location.demo;

import com.baidu.baidulocationdemo.R;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.Poi;
import com.baidu.location.service.LocationService;
import com.baidu.location.service.MyLocationService; import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast; import org.apache.http.params.CoreConnectionPNames; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map; import javax.net.ssl.HttpsURLConnection; /***
* 单点定位示例,用来展示基本的定位结果,配置在LocationService.java中
* 默认配置也可以在LocationService中修改
* 默认配置的内容自于开发者论坛中对开发者长期提出的疑问内容
*
* @author baidu
*/
public class LocationActivity extends Activity {
private LocationService locationService;
private MyLocationService myLocationService;
private TextView LocationResult;
private Button startLocation;
//private Button query; public static String workid = null; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// -----------demo view config ------------
setContentView(R.layout.location);
LocationResult = (TextView) findViewById(R.id.textView1);
LocationResult.setMovementMethod(ScrollingMovementMethod.getInstance());
startLocation = (Button) findViewById(R.id.addfence);
//query=(Button) findViewById(R.id.query); } /**
* 显示请求字符串
*
* @param str
*/
public void logMsg(String str) {
try {
if (LocationResult != null)
LocationResult.setText(str);
Log.w("warn", str);
//insert(longitude,latitude);
} catch (Exception e) {
e.printStackTrace();
}
} /***
* Stop location service
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
} private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.baidu.location.service.MyLocationService".equals(service.service.getClassName())) {
return true;
}
}
return false;
} @Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("workid", "" + this.isServiceRunning());
if(this.isServiceRunning()){
startLocation.setText(getString(R.string.stoplocation));
}else{
startLocation.setText(getString(R.string.startlocation));
}
startLocation.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (startLocation.getText().toString().equals(getString(R.string.startlocation))) {
LocationResult.setVisibility(View.VISIBLE);
//locationService.start();// 定位SDK
// start之后会默认发起一次定位请求,开发者无须判断isstart并主动调用request
Date date = new Date();
LocationActivity.workid = new SimpleDateFormat("MMddHHmm").format(date);
Intent idintent = new Intent(LocationActivity.this, MyLocationService.class);
idintent.putExtra("workid", LocationActivity.workid);
startService(idintent);
startLocation.setText(getString(R.string.stoplocation));
} else {
//locationService.stop();
stopService(new Intent(LocationActivity.this, MyLocationService.class));
getdialog();
startLocation.setText(getString(R.string.startlocation));
LocationResult.setVisibility(View.INVISIBLE);
}
}
});
} private void getdialog() {
final EditText et = new EditText(this);
final String workid = this.workid;
new AlertDialog.Builder(this).setTitle("请输入面积").setView(et).
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
final String size = et.getText().toString();
new Thread() {
@Override
public void run() {
httpget(workid, size);
}
}.start();
//Log.i("size",size); }
}).show();
} public String httpget(String workid, String size) { String result = "";
BufferedReader in = null;
StringBuilder buf = new StringBuilder("http://www.agribiotech.cn/record/record/sizerecord");
buf.append("?");
buf.append("workid=" + workid + "&");
buf.append("size=" + size); try {
URL url = null;
url = new URL(buf.toString());
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
Map<String, List<String>> map = conn.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
} } catch (IOException e) {
Log.i("warn", e.toString());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result; }
}

相关文章