android通过gps获取定位的位置数据和gps经纬度
package com.action.android_test;
import android.location.location;
import android.location.locationlistener;
import android.location.locationmanager;
import android.os.bundle;
import android.app.activity;
import android.view.menu;
import android.widget.textview;
public class mainactivity extends activity {
private location location=null;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
//获得位置服务的名称
string servicename = this.location_service;
//获得位置服务的管理对象
locationmanager locationmanager = (locationmanager)getsystemservice(servicename);
// 通过gps获取定位的位置数据
location = locationmanager.getlastknownlocation(locationmanager.gps_provider);
updatetonewlocation(location);
/**服务管理对象的监听器*/
//参数1:定位的方式 参数2:监听更新间隔时间(ms) 参数3:监听更新的距离(m) 参数4:监听的方法
locationmanager.requestlocationupdates(locationmanager.gps_provider, 2000, 10, new locationlistener() {
public void onstatuschanged(string provider, int status, bundle extras) {
}
public void onproviderenabled(string provider) {
}
public void onproviderdisabled(string provider) {
}
public void onlocationchanged(location location) {
updatetonewlocation(location);
}
});
}
private void updatetonewlocation(location location) {
textview tv1;
tv1 = (textview) this.findviewbyid(r.id.tv1);
if (location != null) {
double latitude = location.getlatitude();
double longitude= location.getlongitude();
tv1.settext("经纬度为:n"+"维度:" + latitude+ "n经度" + longitude);
} else {
tv1.settext("无法获取地理信息");
}
}
@override
public boolean oncreateoptionsmenu(menu menu) {
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
下一篇: Android创建文件实现对文件监听示例