gpt4 book ai didi

android - onLocationChanged 不适用于真实设备

转载 作者:行者123 更新时间:2023-11-30 03:40:09 26 4
gpt4 key购买 nike

现在我正在使用基于位置的应用程序,但我的问题是位置未在真实设备上更新。那是 onLocationChanged 在真实设备上不工作。但在模拟器上没问题。

    locmngr=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
loclis =new MyLocationListner();
our_location=locmngr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locmngr.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0,loclis);
public class MyLocationListner implements LocationListener{

@Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
L1=location.getLatitude();
L2=location.getLongitude();
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
// Toast.makeText(getApplicationContext(), currentDateTimeString, Toast.LENGTH_SHORT).show();
DatabaseHandlerActivity db = new DatabaseHandlerActivity(getApplicationContext());
db.addLocation(new Locations_viewer(L1.toString(), L2.toString(), currentDateTimeString));

Toast.makeText(getApplicationContext(), "New Location "+"Latitude ="+L1+
" Longitude ="+L2, Toast.LENGTH_LONG).show();


Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());


ParseUser.getCurrentUser().put("Latitude", L1.toString());
ParseUser.getCurrentUser().put("Longitude", L2.toString());

ParseUser.getCurrentUser().put("Time", formattedDate);


ParseUser.getCurrentUser().saveInBackground();

// Toast.makeText(getApplicationContext(), "Latitude ="+L1, Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Longitude ="+L2, Toast.LENGTH_LONG).show();



}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@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

}
}

请帮帮我。

最佳答案

我只是使用 NETWORK_PROVIDER 而不是 GPS_PROVIDER。因为 GPS 并非在所有位置都可用。就我而言,它完美地解决了我的问题。我们只需要在我们的手机中建立有效的互联网连接。

关于android - onLocationChanged 不适用于真实设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com