Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
soleMemory committed May 1, 2016
1 parent 14da11b commit 3b743ac
Showing 1 changed file with 62 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public class Net1314080903234GPS extends ListActivity {
private Button button;
private static final String TAG="GpsActivity";

//获取当前时间
//鑾峰彇褰撳墠鏃堕棿
SimpleDateFormat formatter= new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);

//设置按钮
//璁剧疆鎸夐挳
private View.OnClickListener btnListener= new View.OnClickListener() {
@Override
@Override
public void onClick(View v) {
if(getListView().getVisibility()==View.VISIBLE) {
Toast.makeText(getApplicationContext(),"显示历史记录",Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"鏄剧ず鍘嗗彶璁板綍",Toast.LENGTH_SHORT).show();
setListAdapter(adapter);
refresh();
}
Expand All @@ -64,8 +64,6 @@ public void onClick(View v) {
};




@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -74,7 +72,7 @@ public void onCreate(Bundle savedInstanceState) {
button.setOnClickListener(btnListener);


//读数据库
//璇绘暟鎹簱
db =new Db(this);
dbRead = db.getReadableDatabase();
dbWrite=db.getWritableDatabase();
Expand All @@ -91,40 +89,40 @@ public void onCreate(Bundle savedInstanceState) {
editText=(EditText)findViewById(R.id.editText);
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

//判断GPS是否正常启动
//鍒ゆ柇GPS鏄惁姝e父鍚姩
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
//返回开启GPS导航设置界面
Toast.makeText(this, "璇峰紑鍚疓PS瀵艰埅...", Toast.LENGTH_SHORT).show();
//杩斿洖寮�鍚疓PS瀵艰埅璁剧疆鐣岄潰
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent,0);
return;
}

//为获取地理位置信息时设置查询条件
//涓鸿幏鍙栧湴鐞嗕綅缃俊鎭椂璁剧疆鏌ヨ鏉′欢
String bestProvider = lm.getBestProvider(getCriteria(), true);
//获取位置信息
//如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
//鑾峰彇浣嶇疆淇℃伅
//濡傛灉涓嶈缃煡璇㈣姹傦紝getLastKnownLocation鏂规硶浼犱汉鐨勫弬鏁颁负LocationManager.GPS_PROVIDER
Location location= lm.getLastKnownLocation(bestProvider);
updateView(location);
//监听状态
//鐩戝惉鐘舵��
lm.addGpsStatusListener(listener);
//绑定监听,有4个参数
//参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
//参数2,位置信息更新周期,单位毫秒
//参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
//参数4,监听
//备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新

// 1秒更新一次,或最小位移变化超过1米更新一次;
//注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置
//缁戝畾鐩戝惉锛屾湁4涓弬鏁�
//鍙傛暟1锛岃澶囷細鏈塆PS_PROVIDER鍜孨ETWORK_PROVIDER涓ょ
//鍙傛暟2锛屼綅缃俊鎭洿鏂板懆鏈燂紝鍗曚綅姣
//鍙傛暟3锛屼綅缃彉鍖栨渶灏忚窛绂伙細褰撲綅缃窛绂诲彉鍖栬秴杩囨鍊兼椂锛屽皢鏇存柊浣嶇疆淇℃伅
//鍙傛暟4锛岀洃鍚�
//澶囨敞锛氬弬鏁�2鍜�3锛屽鏋滃弬鏁�3涓嶄负0锛屽垯浠ュ弬鏁�3涓哄噯锛涘弬鏁�3涓�0锛屽垯閫氳繃鏃堕棿鏉ュ畾鏃舵洿鏂帮紱涓よ�呬负0锛屽垯闅忔椂鍒锋柊

// 1绉掓洿鏂颁竴娆★紝鎴栨渶灏忎綅绉诲彉鍖栬秴杩�1绫虫洿鏂颁竴娆★紱
//娉ㄦ剰锛氭澶勬洿鏂板噯纭害闈炲父浣庯紝鎺ㄨ崘鍦╯ervice閲岄潰鍚姩涓�涓猅hread锛屽湪run涓璼leep(10000);鐒跺悗鎵цhandler.sendMessage(),鏇存柊浣嶇疆
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
}

//位置监听
//浣嶇疆鐩戝惉
private LocationListener locationListener=new LocationListener() {

/**
* 位置信息变化时触发
* 浣嶇疆淇℃伅鍙樺寲鏃惰Е鍙�
*/
public void onLocationChanged(Location location) {
updateView(location);
Expand All @@ -139,42 +137,42 @@ public void onLocationChanged(Location location) {


//
// Log.i(TAG, "时间:" + location.getTime());
// Log.i(TAG, "经度:"+location.getLongitude());
// Log.i(TAG, "纬度:"+location.getLatitude());
// Log.i(TAG, "海拔:"+location.getAltitude());
// Log.i(TAG, "鏃堕棿锛�" + location.getTime());
// Log.i(TAG, "缁忓害锛�"+location.getLongitude());
// Log.i(TAG, "绾害锛�"+location.getLatitude());
// Log.i(TAG, "娴锋嫈锛�"+location.getAltitude());
}

/**
* GPS状态变化时触发
* GPS鐘舵�佸彉鍖栨椂瑙﹀彂
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
//GPS状态为可见时
//GPS鐘舵�佷负鍙鏃�
case LocationProvider.AVAILABLE:
Log.i(TAG, "当前GPS状态为可见状态");
Log.i(TAG, "褰撳墠GPS鐘舵�佷负鍙鐘舵��");
break;
//GPS状态为服务区外时
//GPS鐘舵�佷负鏈嶅姟鍖哄鏃�
case LocationProvider.OUT_OF_SERVICE:
Log.i(TAG, "当前GPS状态为服务区外状态");
Log.i(TAG, "褰撳墠GPS鐘舵�佷负鏈嶅姟鍖哄鐘舵��");
break;
//GPS状态为暂停服务时
//GPS鐘舵�佷负鏆傚仠鏈嶅姟鏃�
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.i(TAG, "当前GPS状态为暂停服务状态");
Log.i(TAG, "褰撳墠GPS鐘舵�佷负鏆傚仠鏈嶅姟鐘舵��");
break;
}
}

/**
* GPS开启时触发
* GPS寮�鍚椂瑙﹀彂
*/
public void onProviderEnabled(String provider) {
Location location=lm.getLastKnownLocation(provider);
updateView(location);
}

/**
* GPS禁用时触发
* GPS绂佺敤鏃惰Е鍙�
*/
public void onProviderDisabled(String provider) {
updateView(null);
Expand All @@ -183,85 +181,85 @@ public void onProviderDisabled(String provider) {

};

//状态监听
//鐘舵�佺洃鍚�
GpsStatus.Listener listener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch (event) {
//第一次定位
//绗竴娆″畾浣�
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.i(TAG, "第一次定位");
Log.i(TAG, "绗竴娆″畾浣�");
break;
//卫星状态改变
//鍗槦鐘舵�佹敼鍙�
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.i(TAG, "卫星状态改变");
//获取当前状态
Log.i(TAG, "鍗槦鐘舵�佹敼鍙�");
//鑾峰彇褰撳墠鐘舵��
GpsStatus gpsStatus=lm.getGpsStatus(null);
//获取卫星颗数的默认最大值
//鑾峰彇鍗槦棰楁暟鐨勯粯璁ゆ渶澶у��
int maxSatellites = gpsStatus.getMaxSatellites();
//创建一个迭代器保存所有卫星
//鍒涘缓涓�涓凯浠e櫒淇濆瓨鎵�鏈夊崼鏄�
Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();
int count = 0;
while (iters.hasNext() && count <= maxSatellites) {
GpsSatellite s = iters.next();
count++;
}
System.out.println("搜索到:"+count+"颗卫星");
System.out.println("鎼滅储鍒帮細"+count+"棰楀崼鏄�");
break;
//定位启动
//瀹氫綅鍚姩
case GpsStatus.GPS_EVENT_STARTED:
Log.i(TAG, "定位启动");
Log.i(TAG, "瀹氫綅鍚姩");
break;
//定位结束
//瀹氫綅缁撴潫
case GpsStatus.GPS_EVENT_STOPPED:
Log.i(TAG, "定位结束");
Log.i(TAG, "瀹氫綅缁撴潫");
break;
}
};
};

/**
* 实时更新文本内容
* 瀹炴椂鏇存柊鏂囨湰鍐呭
*
* @param location
*/
private void updateView(Location location){
if(location!=null){
editText.setText("位置信息\n\n经度:");
editText.setText("浣嶇疆淇℃伅\n\n缁忓害锛�");
editText.append(String.valueOf(location.getLongitude()));
editText.append("\n纬度:");
editText.append("\n绾害锛�");
editText.append(String.valueOf(location.getLatitude()));
editText.append("\n海拔:");
editText.append("\n娴锋嫈锛�");
editText.append(String.valueOf(location.getAltitude()));



editText.append("\n时间:");
editText.append("\n鏃堕棿锛�");
editText.append(str);


}else{
//清空EditText对象
//娓呯┖EditText瀵硅薄
editText.getEditableText().clear();
}
}

/**
* 返回查询条件
* 杩斿洖鏌ヨ鏉′欢
* @return
*/
private Criteria getCriteria(){
Criteria criteria=new Criteria();
//设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
//璁剧疆瀹氫綅绮剧‘搴� Criteria.ACCURACY_COARSE姣旇緝绮楃暐锛孋riteria.ACCURACY_FINE鍒欐瘮杈冪簿缁�
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//设置是否要求速度
//璁剧疆鏄惁瑕佹眰閫熷害
criteria.setSpeedRequired(false);
// 设置是否允许运营商收费
// 璁剧疆鏄惁鍏佽杩愯惀鍟嗘敹璐�
criteria.setCostAllowed(false);
//设置是否需要方位信息
//璁剧疆鏄惁闇�瑕佹柟浣嶄俊鎭�
criteria.setBearingRequired(false);
//设置是否需要海拔信息
//璁剧疆鏄惁闇�瑕佹捣鎷斾俊鎭�
criteria.setAltitudeRequired(false);
// 设置对电源的需求
// 璁剧疆瀵圭數婧愮殑闇�姹�
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
Expand Down

0 comments on commit 3b743ac

Please sign in to comment.