在这里,我使用的是用户的当前位置以及之前存储的位置,并显示它们之间的路径。 我能够获得正确的当前位置,但我正在尝试永久存储在共享首选项中的位置只是在应用程序运行之前存储。删除应用程序并再次打开后,保存的位置将初始化为0.0。
我想在用户按下保存/更改位置按钮后永久存储纬度和经度。
以下是代码:
public class Fragment_ReachHome extends Fragment {
WebView wv;
ProgressDialog pg;
Background bg;
AlertDialog.Builder alert;
ConnectionDetector cd;
MediaPlayer exceptionNotifier;
boolean isInternetPresent = false;
LocationManager locationManager;
LocationListener locationListener;
float latitude;
float longitude;
Location loc;
public static float savelati, savelongi;
Boolean islocationmanagerrequested = false;
Button bsaveorchangeloc, bshowsavedloc;
SharedPreferences pref;
SharedPreferences.Editor edit;
Boolean islocsaved = false, isbpressed = false;
public Fragment_ReachHome() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_reach_home, container, false);
wv=(WebView)rootView.findViewById(R.id.webView1);
alert = new AlertDialog.Builder(getActivity());
cd = new ConnectionDetector(getActivity());
locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getActivity(), "Loading... Please Wait..", Toast.LENGTH_LONG).show();
locationListener = new LocationListener() { //-----X0-- THIS SHOULD ALWAYS COME FIRST THEN X1 SHOULD BE WRITTEN
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
loc = location;
latitude = Float.valueOf((float) loc.getLatitude());
longitude = Float.valueOf((float) loc.getLongitude());
if(isbpressed){
Toast.makeText(getActivity(), "on loc change", Toast.LENGTH_LONG).show();
}
bg = new Background();
bg.execute();
}
};
try {
isInternetPresent = cd.isConnectingToInternet();
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(),
Toast.LENGTH_LONG).show();
}
if(isInternetPresent){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
}else{
exceptionNotifier = MediaPlayer.create(getActivity(), R.raw.notify);
exceptionNotifier.start();
alert.setTitle("Alert!");
alert.setMessage("Internet Not Present..! ");
alert.setCancelable(true);
alert.setPositiveButton("Ok!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
exceptionNotifier.release();
}
}).show();
}
pref = this.getActivity().getSharedPreferences("", 0);
edit=pref.edit();
bsaveorchangeloc =(Button)rootView.findViewById(R.id.button1);
bshowsavedloc =(Button)rootView.findViewById(R.id.button2);
islocsaved = pref.getBoolean("locsaved", false);
bsaveorchangeloc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progress();
try {
isInternetPresent = cd.isConnectingToInternet();
} catch (Exception e) {
Toast.makeText(getActivity(), e.toString(),
Toast.LENGTH_LONG).show();
}
if(isInternetPresent){
//ONLY AFTER X0 THIS MUST BE WRITTEN
Toast.makeText(getActivity(), "Saving Location!!", Toast.LENGTH_LONG).show();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
isbpressed = true;
islocationmanagerrequested = true;
savelati = pref.getFloat("latestlati", Float.valueOf((float) loc.getLatitude())); //giving default value as 0
savelongi= pref.getFloat("latestlongi", Float.valueOf((float) loc.getLatitude()));
edit.putBoolean("locsaved", true);
edit.commit();
if(islocationmanagerrequested){
locationManager.removeUpdates(locationListener);
}
pg.dismiss();
}else{
exceptionNotifier = MediaPlayer.create(getActivity(), R.raw.notify);
exceptionNotifier.start();
alert.setTitle("Alert!");
alert.setMessage("Internet Not Present..! ");
alert.setCancelable(true);
alert.setPositiveButton("Ok!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
exceptionNotifier.release();
}
}).show();
}
}
});
bshowsavedloc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), savelati+","+savelongi, Toast.LENGTH_LONG).show();
}
});
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.setWebViewClient(new MyWebClient());
return rootView;
}
public void progress(){
pg = new ProgressDialog(getActivity());
pg.setTitle("");
pg.setMessage("Please Wait.........");
pg.setCancelable(false);
pg.setIndeterminate(true);
pg.show();
}
class Background extends AsyncTask<Void, Void, Void>
{
@SuppressLint("SetJavaScriptEnabled")
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try{
if(islocsaved){
wv.loadUrl("http://maps.google.com/maps?saddr="+savelati+","+savelongi+"&daddr="+loc.getLatitude()+","+loc.getLongitude());
}else{
pg.dismiss();
exceptionNotifier = MediaPlayer.create(getActivity(), R.raw.notify);
exceptionNotifier.start();
alert.setTitle("Alert!");
alert.setMessage("Your home location is not saved yet..! ");
alert.setCancelable(true);
alert.setPositiveButton("Ok!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
exceptionNotifier.release();
}
}).show();
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getActivity(), latitude+","+longitude, Toast.LENGTH_LONG).show();
locationManager.removeUpdates(locationListener);
}
@Override
protected void onPreExecute() {
progress();
}
}
public class MyWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
pg.dismiss();
}
}
}
请让我知道代码中的正确更改,以便永久存储该位置...
答案 0 :(得分:1)
您可以使用以下代码在preference
或任何onPause()
事件Button click
上保存价值。
edit.putFloat("latestlati", Float.valueOf((float) loc.getLatitude()))
edit.putFloat("latestlongi", Float.valueOf((float) loc.getLatitude()));
edit.putBoolean("locsaved", true);
edit.commit();
您可以使用以下代码从preference
onResume()
获取保存的值。
savelati = pref.getFloat("latestlati", 0.0);
savelongi= pref.getFloat("latestlongi", 0.0);
islocsaved=edit.getBoolean("locsaved", true);
有关详细信息,请转到Android Shared preferences example或http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
答案 1 :(得分:0)
您需要先在共享偏好中保存经度和纬度的值。
edit.putFloat("latestlati", Float.valueOf((float) loc.getLatitude());
edit.putFloat("latestlongi", Float.valueOf((float) loc.getLongitude);
答案 2 :(得分:0)
为了更好的建议,您必须全局采用SharedPreferance实例
然后在您的onLocationChanged(Location location)
更改方法中,您可以执行此代码
edit.putFloat("latestlati", Float.valueOf((float) loc.getLatitude()))
edit.putFloat("latestlongi", Float.valueOf((float) loc.getLatitude()));
edit.putBoolean("locsaved", false);
edit.commit();
因此,每个位置都会更改您的位置值,并且在进入您的活动后,您始终会获得刷新/最新值。
使用onCreate() or onResume()
即可获得
savelati = pref.getFloat("latestlati", 0.0);
savelongi= pref.getFloat("latestlongi", 0.0);
islocsaved=edit.getBoolean("locsaved", false);