android GPS和基站定位程序及源码.rar 实现基站定位,以及WIFI定位,以及GPS定位
package com.cking;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.List;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.location.LocationProvider;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class ReadGpsDataActivity extends Activity { /** Called when the activity is first created. */int count=0;private TextView tv;private Button bu;private Button bu01;private Button bu02;private Button bu03;double wei;double jing;String name;GpsData gpsData;Location c=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bu=(Button)findViewById(R.id.bu); bu01=(Button)findViewById(R.id.bu01); bu02=(Button)findViewById(R.id.bu02); bu03=(Button)findViewById(R.id.bu03); tv=(TextView)findViewById(R.id.tv); Intent in=getIntent(); if(in.hasExtra("weidu")) { wei=in.getDoubleExtra("weidu", 0.0); jing=in.getDoubleExtra("jingdu", 0.0); name=in.getStringExtra("name"); String weis= String.valueOf(wei); String jins= String.valueOf(jing);// String con=weis "\n" jins "\n" name; tv.setText(con); } bu.setOnClickListener(new OnClickListener(){public void onClick(View v) {// TODO Auto-generated method stubIntent in=new Intent();in.setClass(ReadGpsDataActivity.this, ReadGpsDataService.class);startService(in);} }); bu01.setOnClickListener(new OnClickListener(){public void onClick(View v) throws IllegalArgumentException, SecurityException {Criteria criteria=new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);//精确速度 criteria.setAltitudeRequired(false);//海拔 criteria.setBearingRequired(false);//方向 criteria.setCostAllowed(false);//是否计费 criteria.setPowerRequirement(Criteria.POWER_LOW); long ben = System.currentTimeMillis(); System.out.println("ben time " ben); final LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener llistener=new LocationListener(){ public void onLocationChanged(Location location) { double lat; double lng; if (location != null) { lat=location.getLatitude(); lng=location.getLongitude(); long time=location.getTime(); double altitude=location.getAltitude();//海拔 double accuracy=location.getAccuracy();//精确度 float bearing=location.getBearing();//航向 float speed=location.getSpeed() ; //速度 Calendar c = Calendar.getInstance(); c.setTimeInMillis(time-8*60*60*1000); SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String bf=df.format(c.getTime()); float Lat=(float)lat; float Lng=(float)lng; float alt=(float)altitude; float acc=(float)accuracy; Intent in=new Intent(); in.putExtra("flag", "china activity: "); in.putExtra("time", bf);in.putExtra("jingdu", Lng);in.putExtra("weidu",Lat);in.putExtra("gao", alt);in.putExtra("acc", acc);in.putExtra("spear", bearing);in.putExtra("speed", speed);in.setClass(ReadGpsDataActivity.this,ShowLocat.class);startActivity(in);locationManager.removeUpdates(this); } else { count ; System.out.println("count is " count); } } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub System.out.println("GPS can not be readed!"); } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub System.out.println("GPS can be readed!"); } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub System.out.println("status changed!"); } }; try{ locationManager.requestLocationUpdates("gps", 3000, 1, llistener); long end = System.currentTimeMillis(); System.out.println("end time " end); System.out.println("delay time " (ben-end)); }catch(IllegalArgumentException e){ Intent in=new Intent();in.putExtra("jingdu", 1.0);in.putExtra("weidu",1.0);in.setClass(ReadGpsDataActivity.this,ShowLocat.class);startActivity(in); } catch(RuntimeException e){ Intent in=new Intent();in.putExtra("jingdu", 3.0);in.putExtra("weidu",3.0);in.setClass(ReadGpsDataActivity.this,ShowLocat.class);startActivity(in); } for(int i=1;i<10;i ) { System.out.println(i); Location location = locationManager.getLastKnownLocation("gps"); try { Thread.sleep(3000);//读GPS数据缓冲时间 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//end while locationManager.removeUpdates(llistener); } }); bu02.setOnClickListener(new OnClickListener(){ public void onClick(View v) { LocationManager mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER); double lat; double lng; if (location != null) { //alt=location.getAltitude(); lat=location.getLatitude(); lng=location.getLongitude(); long time=location.getTime(); System.out.println(time); double altitude=location.getAltitude();//海拔 double accuracy=location.getAccuracy();//精确度 float bearing=location.getBearing();//航向 float speed=location.getSpeed(); //速度 Calendar c = Calendar.getInstance(); c.setTimeInMillis(time); SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String bf=df.format(c.getTime()); float Lat=(float)lat; float Lng=(float)lng; float alt=(float)altitude; float acc=(float)accuracy; Intent in=new Intent(); in.putExtra("flag", "china lastLoc: "); in.putExtra("time", bf);in.putExtra("jingdu", Lng);in.putExtra("weidu",Lat);in.putExtra("gao", alt);in.putExtra("acc", acc);in.putExtra("spear", bearing);in.putExtra("speed", speed);in.setClass(ReadGpsDataActivity.this,ShowLocat.class);startActivity(in); } } }); bu03.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub } }); } }
评论