在我的應用程序中,我希望在應用程序啓動期間獲取當前位置。在我的主要活動(第一個活動)中,首先獲取位置(經度和緯度)並保存共享首選項。之後,我放置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" />
如何在啓動應用程序期間獲取當前位置?另外,如果用戶的位置關閉,我應該怎麼做?
,謝謝你的回覆。我現在知道了。我如何確保gotLocation完成?並且比我能放置我的片段。 @Mohamed Embaby – user8654574
使用postDelayed 2000ms或你想要的值在MainActivity中創建一個Runnable,並且如果爲true,則檢查Runnable中的hasAltitudevalue如果false爲false,則調用Runnable並使計數器不需要Runnable在無限循環中調用。 ...如果打開GPS並且有互聯網接入,則首先進行更高效的檢查。 –