我正在使用xamarin android應用程序。我正在嘗試將oncreate方法中的緯度,經度和城市名稱傳遞給服務。但是,我只能在位置更改方法中獲取這些值,並且無法將這些值通過創建傳遞給服務。如何從android xamarin中將onlocationchanged的值轉換爲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());
}
}
}
這將是有益的,如果你有一些更多的描述(和示例代碼)就在這裏,除了您所提供的鏈接。鏈接可能腐爛。 –