我正在开发xamarin android app。我试图在oncreate方法中获取纬度,经度和城市名称以传递给服务。但是我只在onlocation change方法中获取这些值,并且无法在oncreate中获取这些值以传递给服务。
有人可以帮我解决这个问题吗? 感谢。
namespace Myapp
{
[Activity(Label = "Test")]
class Test: AppCompatActivity, ILocationListener
{
LocationManager locMgr;
string tag = "Test";
private Android.Support.V7.Widget.Toolbar mToolBar;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private MyAdapter mAdapter;
public static string latitude = null;
public static string longitude = null;
public static string cityname = null;
protected async override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Android.Resource.Layout.test);
mToolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Android.Resource.Id.toolbar);
SetSupportActionBar(mToolBar);
SupportActionBar.Title = "Test";
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
mRecyclerView = FindViewById<RecyclerView>(Android.Resource.Id.testing);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Contacting server. Please wait...");
progress.SetCancelable(false);
progress.Show();
locMgr = GetSystemService(Context.LocationService) as LocationManager;
if (locMgr.AllProviders.Contains(LocationManager.NetworkProvider)
&& locMgr.IsProviderEnabled(LocationManager.NetworkProvider))
{
locMgr.RequestLocationUpdates(LocationManager.NetworkProvider, 2000, 1, this);
}
else
{
Toast.MakeText(this, "The Network Provider does not exist or is not enabled!", ToastLength.Long).Show();
}
**Here I need to pass the latitude, longitude and city name to my service.**
progress.Dismiss();
mAdapter = new MyAdapter(Count, mRecyclerView, this);
// mAdapter.ItemClick += OnItemClick;
mRecyclerView.SetAdapter(mAdapter);
}
}
public async void GetAddress()
{
Geocoder geocoder = new Geocoder(this);
IList<Address> addressList = await geocoder.GetFromLocationAsync(Convert.ToDouble(latitude), Convert.ToDouble(longitude), 10);
Address address = addressList.FirstOrDefault();
if (address != null)
{
StringBuilder deviceAddress = new StringBuilder();
for (int i = 0; i < address.MaxAddressLineIndex; i++)
{
deviceAddress.Append(address.GetAddressLine(i))
.AppendLine(",");
}
cityname = address.GetAddressLine(1);
// Toast.MakeText(this, address.GetAddressLine(1), ToastLength.Long).Show();
}
else
{
// _addressText.Text = "Unable to determine the address.";
}
}
public void OnLocationChanged(Android.Locations.Location location)
{
// Log.Debug(tag, "Location changed");
// latitude.Text = "Latitude: " + location.Latitude.ToString();
// longitude.Text = "Longitude: " + location.Longitude.ToString();
// provider.Text = "Provider: " + location.Provider.ToString();
latitude = location.Latitude.ToString();
longitude = location.Longitude.ToString();
GetAddress();
// String[] s = cityname.Split(',');
}
public void OnProviderDisabled(string provider)
{
Log.Debug(tag, provider + " disabled by user");
}
public void OnProviderEnabled(string provider)
{
Log.Debug(tag, provider + " enabled by user");
}
public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
Log.Debug(tag, provider + " availability has changed to " + status.ToString());
}
}
}
答案 0 :(得分:0)
与桌面编程不同,在Android上,您无法处理应用程序的生命周期步骤。只有在生命周期发生变化并在那里处理时,您才会收到通知。简而言之,获取最新位置的唯一方法是等待onLocationChanged被调用。
我的建议是在onLocationChanged方法中启动服务或启动服务,然后在调用onLocationChanged时,将位置传达给您的服务。或者您的服务可能是LocationListener并绕过那里的通信。
答案 1 :(得分:-1)
下面是我编写的一个简单示例的链接,该示例演示如何在OnCreate中创建位置请求,然后在OnLocationChanged中获取结果。您已经拥有类成员的经度和纬度变量。您可以在OnLocationChanged中设置这些变量,但需要从OnLocationChanged调用使用这些值执行某些操作的代码。
https://github.com/LCC-CIT/CS235AM-Demos/tree/master/GeolocationDemo/FusedLoationProvider