2017-12-27 1808 views
0

在我的應用程序中,我希望在應用程序啓動期間獲取當前位置。在我的主要活動(第一個活動)中,首先獲取位置(經度和緯度)並保存共享首選項。之後,我放置HomeFragment。我想在應用程序打開時看到HomeFragment。 在我的HomeFragment中,我想獲取sharedPrefreferences,而不是使用位置和緯度信息來計算距離。 但是,這不起作用,當我啓動應用程序時,它在保存SharePreferences之前放置HomeFragment。如何在應用程序啓動期間獲取位置

這裏是我的MainActivity:

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
      MyLocation myLocation = new MyLocation(this); 
      myLocation.getLocation(this, locationResult); 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_main); 

      FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
      tx.replace(R.id.flContent, new HomeFragment()); 
      tx.commit(); 

     public MyLocation.LocationResult locationResult = new MyLocation.LocationResult() { 

      @Override 
      public void gotLocation(Location location) { 
       // TODO Auto-generated method stub 
       double Longitude = location.getLongitude(); 
       double Latitude = location.getLatitude(); 

       try { 
        System.out.println("lat"+Latitude); 
        System.out.println("long"+Longitude); 
        SharedPreferences locationpref = getApplication() 
          .getSharedPreferences("location", MODE_PRIVATE); 
        SharedPreferences.Editor prefsEditor = locationpref.edit(); prefsEditor.commit(); 
        prefsEditor.putString("Longitude", Longitude + ""); 
        prefsEditor.putString("Latitude", Latitude + ""); 


       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }; 

    } 

HomeFragment:

public class HomeFragment extends Fragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 

     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 


     View rootView = inflater.inflate(R.layout.fragment_home, container, false); 
     listView = (ListView) rootView.findViewById(R.id.list); 
     adapter = new CustomListAdapter(getActivity(), restaurantList); 
     listView.setAdapter(adapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Bundle bundle = new Bundle(); 
       bundle.putString("restaurantId", String.valueOf(restaurantList.get(position).getId())); 
       MenuLineItemFragment fragment2 = new MenuLineItemFragment(); 
       FragmentManager fragmentManager = getFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.flContent, fragment2); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 

      } 
     }); 


     SharedPreferences pref = getActivity().getApplication().getSharedPreferences("location", MODE_PRIVATE); 
     latitude = pref.getString("Latitude",""); 
     longitude = pref.getString("Longitude",""); 

     if(i==1){ 
      request(); //I use longitude and latitude information in this method. 
      i++; 
     } 



     return rootView; 

    } 

在我的清單文件,我給的權限。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 

如何在啓動應用程序期間獲取當前位置?另外,如果用戶的位置關閉,我應該怎麼做?

回答

0

一些Location類方法是在後臺完成,如getLongitudegetLatitude,是因爲的getLocation需要時間不要緊代碼Fragment初始化的哪一行或getLocation

在您的代碼: -

你叫getLocation> GPS在後臺運行,以獲得位置>Fragment得到background初始化>getLocation完成並通過返回的值​​>gotLocation被調用。

解決方案: -

第一種方式是初始化gotLocationfragment,這是不推薦的。

方式二sharedPrefreferences保存價值和更新您的UI(片段)時gotLocation結束(可能使用進度條和gotLocati enter code here後隱藏的稱呼)。

+0

,謝謝你的回覆。我現在知道了。我如何確保gotLocation完成?並且比我能放置我的片段。 @Mohamed Embaby – user8654574

+0

使用postDelayed 2000ms或你想要的值在MainActivity中創建一個Runnable,並且如果爲true,則檢查Runnable中的hasAltitudevalue如果false爲false,則調用Runnable並使計數器不需要Runnable在無限循環中調用。 ...如果打開GPS並且有互聯網接入,則首先進行更高效的檢查。 –

相關問題