2015-06-23 57 views
0

我正在使用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()); 
      } 





     } 
    } 

回答

0

與桌面編程不同,在Android上,您無法處理應用程序的生命週期步驟。當生命週期發生變化並在那裏處理時,您只會收到通知。總之,獲取最新位置的唯一方法是等待onLocationChanged被調用。

我的建議是在onLocationChanged方法中啓動服務或啓動服務,然後在調用onLocationChanged時將該位置傳遞給您的服務。或者你的服務可能是一個LocationListener並繞過那裏的通信。

-1

下面是我寫的一個簡單示例的鏈接,顯示瞭如何在OnCreate中創建位置請求,然後在OnLocationChanged中獲取結果。您已經擁有屬於類成員的經度和緯度變量。您可以在OnLocationChanged中設置這些變量,但是需要使用OnLocationChanged調用執行這些值的代碼。

https://github.com/LCC-CIT/CS235AM-Demos/tree/master/GeolocationDemo/FusedLoationProvider

+0

這將是有益的,如果你有一些更多的描述(和示例代碼)就在這裏,除了您所提供的鏈接。鏈接可能腐爛。 –

相關問題