Android-使用onClick()中的Android gps提供程序检索当前位置
我正在尝试制作一个简单的应用程序,其中用户单击一个按钮,几个文本视图显示有关手机的各种信息(型号、电池%、位置、信号强度等)。我在获取当前纬度和经度时遇到问题,因为当我按下按钮时,textview会显示上一个纬度/经度(第一次显示0,0,第二次显示我第一次按下时的位置) 我试图实现的是,当我按下按钮时,激活位置管理器和位置侦听器,并使onClick()方法等待,直到纬度不等于原来的纬度。我尝试了线程、处理程序和异步任务,但什么都没做。有什么建议吗?这就是我的onClick()方法现在的样子:(Infogatherer是一个收集所有信息的类) 这是我的InfoGatherer课程:Android-使用onClick()中的Android gps提供程序检索当前位置,android,gps,location,location-provider,Android,Gps,Location,Location Provider,我正在尝试制作一个简单的应用程序,其中用户单击一个按钮,几个文本视图显示有关手机的各种信息(型号、电池%、位置、信号强度等)。我在获取当前纬度和经度时遇到问题,因为当我按下按钮时,textview会显示上一个纬度/经度(第一次显示0,0,第二次显示我第一次按下时的位置) 我试图实现的是,当我按下按钮时,激活位置管理器和位置侦听器,并使onClick()方法等待,直到纬度不等于原来的纬度。我尝试了线程、处理程序和异步任务,但什么都没做。有什么建议吗?这就是我的onClick()方法现在的样子:(I
package com.example.netmap;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.CellInfoGsm;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
public class InfoGatherer extends Application{
String address,city,country;
int cid,lac,generation=0,ipAddress=0,signalStrngth=0;
private GsmCellLocation location;
private WifiInfo wifiInfo;
private LocationManager lm;
private LocationListener ll;
Geocoder geoc;
static public double Longitude,Latitude=0;
List<Address> addresses;
Context context;
Intent batteryIntent;
TelephonyManager tm;
WifiManager wifimanager;
public InfoGatherer(){
}
public InfoGatherer(Context context){
this.context = context;
}
public String getDate(){
Calendar c = Calendar.getInstance();
return Integer.toString(c.get(Calendar.DAY_OF_MONTH))+"-"+Integer.toString(c.get(Calendar.MONTH))+"-"+Integer.toString(c.get(Calendar.YEAR))+" "+Integer.toString(c.get(Calendar.HOUR_OF_DAY))+":"+Integer.toString(c.get(Calendar.MINUTE))+":"+Integer.toString(c.get(Calendar.SECOND));
}
public String getDeviceName(){
return Build.MANUFACTURER +" "+Build.MODEL;
}
public String getNetworkOp(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getNetworkOperatorName();
}
public float getBatteryStatus() {
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return ((float)batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) / (float)batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)) * 100.0f;
}
public int getGeneration(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getNetworkType();
}
public int getCid(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation)tm.getCellLocation();
return location.getCid();
}
public int getLac(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation)tm.getCellLocation();
return location.getLac();
}
public String getIpAddress() {
// TODO Auto-generated method stub
wifimanager = (WifiManager) context.getSystemService(WIFI_SERVICE);
wifiInfo = wifimanager.getConnectionInfo();
ipAddress = wifiInfo.getIpAddress();
return String.format("%d.%d.%d.%d",(ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
}
public void getLocation(){
/*Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setPowerRequirement(Criteria.POWER_LOW);
String provider = lm.getBestProvider(c, true);*/
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
public class mylocationlistener implements LocationListener{
@Override
public void onLocationChanged(android.location.Location location) {
// TODO Auto-generated method stub
if(location!=null){
Longitude = location.getLongitude();
Latitude = location.getLatitude();
lm.removeUpdates(ll);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
//Pass views as parameters? DIscuss
//DeviceName.setText(String.valueOf(Latitude) +" "+String.valueOf(Longitude));
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
public void getSignalStrength(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener Listener = new phoneStateListener();
tm.listen(Listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
public class phoneStateListener extends PhoneStateListener{
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
signalStrngth = -113 + 2 * signalStrength.getGsmSignalStrength();
}
else
signalStrngth = -113 + 2 * signalStrength.getCdmaDbm();
}
}
static public double getLatitude(){
return Latitude;
}
static public double getLongitude(){
return Longitude;
}
public String getAddress(){
return address;
}
public String getCity(){
return city;
}
public String getCountry(){
return country;
}
public int getDbm(){
return signalStrngth;
}
}
package com.example.netmap;
导入java.io.IOException;
导入java.util.Calendar;
导入java.util.List;
导入java.util.Locale;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.Application;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.location.Address;
导入android.location.Criteria;
导入android.location.Geocoder;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.net.wifi.WifiInfo;
导入android.net.wifi.WifiManager;
导入android.os.BatteryManager;
导入android.os.Build;
导入android.os.Bundle;
导入android.telephony.CellInfoGsm;
导入android.telephony.CellSignalStrengthGsm;
导入android.telephony.PhoneStateListener;
导入android.telephony.SignalStrength;
导入android.telephony.TelephonyManager;
导入android.telephony.gsm.GsmCellLocation;
公共类InfoGatherer扩展了应用程序{
字符串地址、城市、国家;
int-cid,lac,generation=0,ipAddress=0,signalStrngth=0;
私人GsmCellLocation;
私人WifiInfo WifiInfo;
私人位置经理lm;
私人场所;
地理编码器;
静态公共双经度,纬度=0;
列出地址;
语境;
目的:防止暴力;
TelephonyManager tm;
WifiManager WifiManager;
公共信息收集程序(){
}
公共信息收集程序(上下文){
this.context=上下文;
}
公共字符串getDate(){
Calendar c=Calendar.getInstance();
返回Integer.toString(c.get(Calendar.DAY\u OF_MONTH))+“-”+Integer.toString(c.get(Calendar.MONTH))+“-”+Integer.toString(c.get(Calendar.YEAR))+”“+Integer.toString(c.get(Calendar.MINUTE))+“:”+Integer.toString(c.get(Calendar.secondary));
}
公共字符串getDeviceName(){
返回Build.MANUFACTURER+“”+Build.MODEL;
}
公共字符串getNetworkOp(){
tm=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
返回tm.getNetworkOperatorName();
}
公共浮点数getBatteryStatus(){
batteryIntent=context.registerReceiver(null,新的IntentFilter(Intent.ACTION\u BATTERY\u CHANGED));
返回((浮动)BatteryInput.getIntExtra(BatteryManager.EXTRA_LEVEL,-1)/(浮动)BatteryInput.getIntExtra(BatteryManager.EXTRA_SCALE,-1))*100.0f;
}
公共int getGeneration(){
tm=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
返回tm.getNetworkType();
}
公共int getCid(){
tm=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
location=(GsmCellLocation)tm.getCellLocation();
返回位置。getCid();
}
public int getLac(){
tm=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
location=(GsmCellLocation)tm.getCellLocation();
返回位置。getLac();
}
公共字符串getIpAddress(){
//TODO自动生成的方法存根
wifimanager=(wifimanager)context.getSystemService(WIFI_服务);
wifiInfo=wifimanager.getConnectionInfo();
ipAddress=wifiInfo.getIpAddress();
返回字符串.format(“%d.%d.%d.%d”,(ipAddress&0xff),(ipAddress>>8&0xff),(ipAddress>>16&0xff),(ipAddress>>24&0xff));
}
public void getLocation(){
/*标准c=新标准();
c、 设定准确度(标准、准确度和精细度);
c、 设置功率要求(标准功率低);
字符串提供程序=lm.getBestProvider(c,true)*/
lm=(LocationManager)context.getSystemService(context.LOCATION\u服务);
ll=新的mylocationlistener();
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,ll);
}
公共类mylocationlistener实现LocationListener{
@凌驾
public void onLocation已更改(android.location.location){
//TODO自动生成的方法存根
如果(位置!=null){
Longitude=location.getLongitude();
纬度=位置。getLatitude();
lm.删除更新(ll);
}
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
//TODO自动生成的方法存根
//将视图作为参数传递?讨论
//DeviceName.setText(String.valueOf(纬度)+“”+String.valueOf(经度));
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
public void onStatusChanged(字符串提供程序,int状态,
捆绑(附加){
//TODO自动生成的方法存根
}
}
公共交通信号强度(){
tm=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
PhoneStateListener=新的PhoneStateListener();
tm.listen(Listener,PhoneStateListener.L
package com.example.netmap;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.CellInfoGsm;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
public class InfoGatherer extends Application{
String address,city,country;
int cid,lac,generation=0,ipAddress=0,signalStrngth=0;
private GsmCellLocation location;
private WifiInfo wifiInfo;
private LocationManager lm;
private LocationListener ll;
Geocoder geoc;
static public double Longitude,Latitude=0;
List<Address> addresses;
Context context;
Intent batteryIntent;
TelephonyManager tm;
WifiManager wifimanager;
public InfoGatherer(){
}
public InfoGatherer(Context context){
this.context = context;
}
public String getDate(){
Calendar c = Calendar.getInstance();
return Integer.toString(c.get(Calendar.DAY_OF_MONTH))+"-"+Integer.toString(c.get(Calendar.MONTH))+"-"+Integer.toString(c.get(Calendar.YEAR))+" "+Integer.toString(c.get(Calendar.HOUR_OF_DAY))+":"+Integer.toString(c.get(Calendar.MINUTE))+":"+Integer.toString(c.get(Calendar.SECOND));
}
public String getDeviceName(){
return Build.MANUFACTURER +" "+Build.MODEL;
}
public String getNetworkOp(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getNetworkOperatorName();
}
public float getBatteryStatus() {
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return ((float)batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) / (float)batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)) * 100.0f;
}
public int getGeneration(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getNetworkType();
}
public int getCid(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation)tm.getCellLocation();
return location.getCid();
}
public int getLac(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation)tm.getCellLocation();
return location.getLac();
}
public String getIpAddress() {
// TODO Auto-generated method stub
wifimanager = (WifiManager) context.getSystemService(WIFI_SERVICE);
wifiInfo = wifimanager.getConnectionInfo();
ipAddress = wifiInfo.getIpAddress();
return String.format("%d.%d.%d.%d",(ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
}
public void getLocation(){
/*Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setPowerRequirement(Criteria.POWER_LOW);
String provider = lm.getBestProvider(c, true);*/
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
public class mylocationlistener implements LocationListener{
@Override
public void onLocationChanged(android.location.Location location) {
// TODO Auto-generated method stub
if(location!=null){
Longitude = location.getLongitude();
Latitude = location.getLatitude();
lm.removeUpdates(ll);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
//Pass views as parameters? DIscuss
//DeviceName.setText(String.valueOf(Latitude) +" "+String.valueOf(Longitude));
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
public void getSignalStrength(){
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener Listener = new phoneStateListener();
tm.listen(Listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
public class phoneStateListener extends PhoneStateListener{
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
signalStrngth = -113 + 2 * signalStrength.getGsmSignalStrength();
}
else
signalStrngth = -113 + 2 * signalStrength.getCdmaDbm();
}
}
static public double getLatitude(){
return Latitude;
}
static public double getLongitude(){
return Longitude;
}
public String getAddress(){
return address;
}
public String getCity(){
return city;
}
public String getCountry(){
return country;
}
public int getDbm(){
return signalStrngth;
}
}
infogatherer.getLocation();
Location.setText(String.valueOf(InfoGatherer.getLatitude()+","+InfoGatherer.getLatitude()));
private void updateLocationText(double oldLat, double oldLong) {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
boolean isPositionChanged = false;
double lat;
double long;
while (!isPositionChanged) {
lat = InfoGatherer.getLatitude();
long = InfoGatherer.getLongitude();
if(lat != oldLat || long != oldLong)isPositionChanged = true;
}
handler.post(new Runnable(){
public void run() {
Location.setText(String.valueOf(InfoGatherer.getLatitude()+","+InfoGatherer.getLatitude()));
});
}
}
};
new Thread(runnable).start();
}
Location.setText(String.valueOf(InfoGatherer.getLatitude()+","+InfoGatherer.getLatitude()));
Location.postDelayed(new Runnable() {
@Override
public void run() {
Location.setText(String.valueOf(InfoGatherer.getLatitude()+","+InfoGatherer.getLatitude()));
Location.postInvalidate();//Try without, may be not necessary
}
}, 3000);//Change if need
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
.........
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener(){
@Override
public void onLocationChanged(android.location.Location location) {
if(location!=null){
Location.setText(String.valueOf(location.getLatitude()+","+location.getLatitude()));
lm.removeUpdates(listener);
}
}
}
}
infogatherer.getLocation();
Location.setText(String.valueOf(InfoGatherer.getLatitude()+","+InfoGatherer.getLatitude()));
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
private class LocationThread extends AsyncTask<Context, Void, Void> {
protected void onPreExecute() {
infogatherer.startLocationListener();
}
@Override
protected Void doInBackground(Context... params) {
while (!infogatherer.getGo()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(final Void unused) {
//Do whatever you wanna do after you get location
}
}
public class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(android.location.Location location) {
if(location != null){
Longitude = location.getLongitude();
Latitude = location.getLatitude();
lm.removeUpdates(ll);
go = true;
}
}