Activity 加载时获取位置(在 onCreate 中)
Fetch Location when Activity Loads(in onCreate)
我尝试使用在我的 MainActivity 的 onCreate 函数中初始化的 locationlistener。
在该函数中,还必须询问位置,但在调试时我发现,监听器在函数 运行 通过后开始提供坐标。等了也没用。
如何在创建 activity 时获取位置,或者如何通过线程更改活动视图?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
if(data.gB_GPS_searched == false) {
data.gB_GPS_searched = true;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
data.locationListener = new GPS();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, data.locationListener);
}
data.set_pier("Bodenwerder");
data.set_pier(data.get_closest_pier());
String cityname = data.get_pier_name();
if(cityname != null)
((TextView)findViewById(R.id.current_city)).setText(cityname);
}
监听器基本上是这样的。
public class GPS implements android.location.LocationListener {
@Override
public void onLocationChanged(Location location) {
if(data.get_gps) {
try {
data.Latitute = ((Double)(location.getLatitude()*100)).intValue();
data.Longitute = ((Double)(location.getLongitude()*100)).intValue();
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
...
}
我正在使用的数据 class 用于保存和接收 t运行sfering ... 数据
public class data {
public static GPS locationListener;
public static int Latitute = 0;
public static int Longitute = 0;
public static boolean gB_GPS_searched = false;
private static Map<String,int[]> coordinates = new HashMap<String,int[]>();
private static int gI_current_city = -1;
private static List<String> g_piernames = new ArrayList<>();
public static void set_pier(String pS_piername)
{
for(int i = 0; i < g_piernames.size(); i++)
if(g_piernames.get(i).equals(pS_piername))
{
gI_current_city = i;
i = g_piernames.size();
}
}
public static String get_pier_name()
{
return g_piernames.get(gI_current_city);
}
public static String get_closest_pier()
{
String cityname = "";
int x = -1;
int y = -1;
for (Object key : coordinates.keySet().toArray() ) {
if(x == -1)
{
x = coordinates.get(key.toString())[0];
y = coordinates.get(key.toString())[1];
cityname = key.toString();
}else{
if(pyth(Math.abs(Latitute-x),Math.abs(Longitute-y)) > pyth(Math.abs(Latitute-coordinates.get(key.toString())[0]),Math.abs(Longitute-coordinates.get(key.toString())[1])))
{
x = coordinates.get(key.toString())[0];
y = coordinates.get(key.toString())[1];
cityname = key.toString();
}
}
}
return cityname;
}
private static int pyth(int a, int b)
{
return ((Double)Math.sqrt(a*a+b*b)).intValue();
}
}
我建议使用 PrivacyStreams 库来获取位置。
将以下两行添加到 app/build.gradle
文件中的 dependencies
:
dependencies {
compile 'com.github.privacystreams:privacystreams-core:0.1.3'
compile 'com.google.android.gms:play-services-location:10.2.1'
}
为AndroidManifest.xml
添加位置权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在您的 onCreate
方法中:
Globals.LocationConfig.useGoogleService = true;
UQI uqi = new UQI(this);
uqi.getData(Geolocation.asUpdates(Geolocation.LEVEL_EXACT), Purpose.UTILITY("<your purpose of getting location>"))
.forEach(Geolocation.LAT_LON, new Callback<LatLon>() {
protected void onInput(LatLon latLon) {
data.Latitute = ((Double) (latLon.getLatitude() * 100)).intValue();
data.Longitute = ((Double) (latLon.getLongitude() * 100)).intValue();
// Do something with the coordinates
}
});
我尝试使用在我的 MainActivity 的 onCreate 函数中初始化的 locationlistener。 在该函数中,还必须询问位置,但在调试时我发现,监听器在函数 运行 通过后开始提供坐标。等了也没用。
如何在创建 activity 时获取位置,或者如何通过线程更改活动视图?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
if(data.gB_GPS_searched == false) {
data.gB_GPS_searched = true;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
data.locationListener = new GPS();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, data.locationListener);
}
data.set_pier("Bodenwerder");
data.set_pier(data.get_closest_pier());
String cityname = data.get_pier_name();
if(cityname != null)
((TextView)findViewById(R.id.current_city)).setText(cityname);
}
监听器基本上是这样的。
public class GPS implements android.location.LocationListener {
@Override
public void onLocationChanged(Location location) {
if(data.get_gps) {
try {
data.Latitute = ((Double)(location.getLatitude()*100)).intValue();
data.Longitute = ((Double)(location.getLongitude()*100)).intValue();
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
...
}
我正在使用的数据 class 用于保存和接收 t运行sfering ... 数据
public class data {
public static GPS locationListener;
public static int Latitute = 0;
public static int Longitute = 0;
public static boolean gB_GPS_searched = false;
private static Map<String,int[]> coordinates = new HashMap<String,int[]>();
private static int gI_current_city = -1;
private static List<String> g_piernames = new ArrayList<>();
public static void set_pier(String pS_piername)
{
for(int i = 0; i < g_piernames.size(); i++)
if(g_piernames.get(i).equals(pS_piername))
{
gI_current_city = i;
i = g_piernames.size();
}
}
public static String get_pier_name()
{
return g_piernames.get(gI_current_city);
}
public static String get_closest_pier()
{
String cityname = "";
int x = -1;
int y = -1;
for (Object key : coordinates.keySet().toArray() ) {
if(x == -1)
{
x = coordinates.get(key.toString())[0];
y = coordinates.get(key.toString())[1];
cityname = key.toString();
}else{
if(pyth(Math.abs(Latitute-x),Math.abs(Longitute-y)) > pyth(Math.abs(Latitute-coordinates.get(key.toString())[0]),Math.abs(Longitute-coordinates.get(key.toString())[1])))
{
x = coordinates.get(key.toString())[0];
y = coordinates.get(key.toString())[1];
cityname = key.toString();
}
}
}
return cityname;
}
private static int pyth(int a, int b)
{
return ((Double)Math.sqrt(a*a+b*b)).intValue();
}
}
我建议使用 PrivacyStreams 库来获取位置。
将以下两行添加到
app/build.gradle
文件中的dependencies
:dependencies { compile 'com.github.privacystreams:privacystreams-core:0.1.3' compile 'com.google.android.gms:play-services-location:10.2.1' }
为
AndroidManifest.xml
添加位置权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在您的
onCreate
方法中:Globals.LocationConfig.useGoogleService = true; UQI uqi = new UQI(this); uqi.getData(Geolocation.asUpdates(Geolocation.LEVEL_EXACT), Purpose.UTILITY("<your purpose of getting location>")) .forEach(Geolocation.LAT_LON, new Callback<LatLon>() { protected void onInput(LatLon latLon) { data.Latitute = ((Double) (latLon.getLatitude() * 100)).intValue(); data.Longitute = ((Double) (latLon.getLongitude() * 100)).intValue(); // Do something with the coordinates } });