2014-09-23 72 views
3

活動啓動後,GPS已啓用並抓取用戶的緯度,經度並將其報告爲地理座標解析。問題在於GPS持續在後臺運行並不斷向解析位置報告。我只想讓GPS抓住位置信息一次,然後立即停下來,就像我自己一樣,我注意到我已經在一天中達到了約20k的解析請求,我認爲它是因爲它在後臺持續運行。使用GPS位置

下面是活動代碼:

public class MoodActivity extends Activity { 

    private FeedbackDialog feedBack; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_mood); 
     feedBack = new FeedbackDialog(this, ""); 
     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
      LocationListener mlocListener = new MyLocationListener(); 

      mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 







     final TextView teating = (TextView) this.findViewById(R.id.tdinning); 
     teating.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class)); 
      } 
     }); 

     final ImageView ieating = (ImageView) this.findViewById(R.id.idinning); 
     ieating.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class)); 
      } 
     }); 

     final TextView tdrinks = (TextView) this.findViewById(R.id.tcasual); 
     tdrinks.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class)); 
      } 
     }); 

     final ImageView idrinks = (ImageView) this.findViewById(R.id.icasual); 
     idrinks.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class)); 
      } 
     }); 

     final TextView tshows = (TextView) this.findViewById(R.id.tshows); 
     tshows.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, EntertainmentEventsActivity.class)); 
      } 
     }); 

     final ImageView ishows = (ImageView) this.findViewById(R.id.ishows); 
     ishows.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, EntertainmentEventsActivity.class)); 
      } 
     }); 

     final TextView tarts = (TextView) this.findViewById(R.id.tculture); 
     tarts.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class)); 
      } 
     }); 

     final ImageView iarts = (ImageView) this.findViewById(R.id.iculture); 
     iarts.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class)); 
      } 
     }); 

     final Button viewall = (Button) this.findViewById(R.id.brandom); 
     viewall.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class)); 
      } 
     }); 
    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_activity_actions, menu); 
     getActionBar().setDisplayShowTitleEnabled(false); 



     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle presses on the action bar items 
     switch (item.getItemId()) { 
      case R.id.pageExperience: 
       openPageExperience(); 
       return true; 
      case R.id.pageMessaging: 
       openPageMessage(); 
       return true; 

      case R.id.pageEventsBooking: 
       openPageBook(); 
       return true; 

      case R.id.pageProfile: 
       openPageProfile(); 
       return true; 

      case R.id.pageReport: 
       openPageReport(); 
       return true; 

      case R.id.pageAbout: 
       openPageAbout(); 
       return true; 

      case R.id.pageLogout: 
       openPageLogout(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    private void openPageLogout() { 
     // TODO Auto-generated method stub 
     //Now call logout 
     ParseUser.logOut(); 
     Intent intent = new Intent(getApplicationContext(), LoginActivity.class); 
     startActivity(intent); 
    } 



    private void openPageAbout() { 
     // TODO Auto-generated method stub 

    } 

    private void openPageReport() { 
     // TODO Auto-generated method stub 
     FeedbackSettings feedbackSettings = new FeedbackSettings(); 

     //SUBMIT-CANCEL BUTTONS 
     feedbackSettings.setCancelButtonText("No"); 
     feedbackSettings.setSendButtonText("Send"); 

     //DIALOG TEXT 
     feedbackSettings.setText("Hey, would you like to give us some feedback so that we can improve your experience?"); 
     feedbackSettings.setYourComments("Type your question here..."); 
     feedbackSettings.setTitle("Feedback Dialog Title"); 

     //TOAST MESSAGE 
     feedbackSettings.setToast("Thank you so much!"); 
     feedbackSettings.setToastDuration(Toast.LENGTH_SHORT); // Default 
     feedbackSettings.setToastDuration(Toast.LENGTH_LONG); 

     //RADIO BUTTONS 
     feedbackSettings.setRadioButtons(false); // Disables radio buttons 
     feedbackSettings.setBugLabel("Bug"); 
     feedbackSettings.setIdeaLabel("Idea"); 
     feedbackSettings.setQuestionLabel("Question"); 

     //RADIO BUTTONS ORIENTATION AND GRAVITY 
     feedbackSettings.setOrientation(LinearLayout.HORIZONTAL); // Default 
     feedbackSettings.setOrientation(LinearLayout.VERTICAL); 
     feedbackSettings.setGravity(Gravity.RIGHT); // Default 
     feedbackSettings.setGravity(Gravity.LEFT); 
     feedbackSettings.setGravity(Gravity.CENTER); 

     //SET DIALOG MODAL 
     feedbackSettings.setModal(true); //Default is false 

     //DEVELOPER REPLIES 
     feedbackSettings.setReplyTitle("Message from the Developer"); 
     feedbackSettings.setReplyCloseButtonText("Close"); 
     feedbackSettings.setReplyRateButtonText("RATE!"); 

     //DEVELOPER CUSTOM MESSAGE (NOT SEEN BY THE END USER) 
     feedbackSettings.setDeveloperMessage("This is a custom message that will only be seen by the developer!"); 
     feedBack.show(); 
    } 

    private void openPageProfile() { 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(this, profileDetailsActivity.class); 
     startActivity(intent); 

    } 

    private void openPageBook() { 
     // TODO Auto-generated method stub 

    } 

    private void openPageMessage() { 
     // TODO Auto-generated method stub 

    } 

    private void openPageExperience() { 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(this, MoodActivity.class); 
     startActivity(intent); 
    } 

    public class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location loc) { 
      // TODO Auto-generated method stub 
      double lati = loc.getLatitude(); 
      double longi = loc.getLongitude(); 

      ParseUser currentUser = ParseUser.getCurrentUser(); 
      currentUser.saveInBackground(); 

      ParseGeoPoint point = new ParseGeoPoint(lati, longi); 
      currentUser.put("location", point); 

      currentUser.saveInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        setProgressBarIndeterminateVisibility(false); 

        if (e == null) { 
         // Success! 

        } else { 

        } 
       } 
      }); 

     } 

      @Override 
      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show(); 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 


      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 
       // TODO Auto-generated method stub 

      } 
      } 
} 

如果你需要任何澄清,讓我知道。 在此先感謝。

更新 我試過使用單一更新來代替,但它似乎並沒有工作。

下的onCreate

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
      LocationListener mlocListener = new MyLocationListener(); 


      mlocManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mlocListener, Looper.myLooper()); 

public class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location loc) { 
      // TODO Auto-generated method stub 
      double lati = loc.getLatitude(); 
      double longi = loc.getLongitude(); 

      ParseUser currentUser = ParseUser.getCurrentUser(); 
      currentUser.saveInBackground(); 

      ParseGeoPoint point = new ParseGeoPoint(lati, longi); 
      currentUser.put("location", point); 

      currentUser.saveInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        setProgressBarIndeterminateVisibility(false); 

        if (e == null) { 
         // Success! 

        } else { 

        } 
       } 
      }); 

     } 

      @Override 
      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show(); 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 


      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 
       // TODO Auto-generated method stub 

      } 
      } 
} 

回答

1

當你通過在調用中給出了LocationListener的調用LocationManager.requestLocationUpdates()時,系統會不斷地提供位置更新。

如果你有興趣在獲取只有一個位置更新,你有兩個選擇:

  1. 使用LocationManager.requestSingleUpdate()
  2. 使用LocationManager.requestLocationUpdates(),但接收的第一次更新後取消更新。

我將與2繼續,因爲你已經有一些code.You首先需要聲明的變量mLocManager是一個類字段,而不是在本地的onCreate聲明它()。然後您需要在您的LocationListener中添加以下代碼。

public class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location loc) { 

      double lati = loc.getLatitude(); 
      double longi = loc.getLongitude(); 

      mlocManager.remoteUpdates(this); // ADD THIS STATEMENT 

      // The rest of your code goes here ... 

     } 

    } 
+0

感謝您的迅速回復和澄清。出於好奇,第一個選項將如何工作? – John 2014-09-23 06:23:19

+0

我嘗試使用單一更新來代替,但它似乎沒有工作。我已經在我的初始文章 – John 2014-09-23 15:17:01

+1

@ user3907211的更新部分中包含了我所做的工作:很難說requestSingleUpdate()有什麼問題。 requestLocationUpdates()是否適合你?你確定你有GPS/WiFi連接嗎? – stackoverflowuser2010 2014-09-23 18:01:03

2

你必須要找到像

mlocManager.removeUpdates(mlocListener); 

辦法removeupdates你把你的解析信息

+0

感謝您的回覆。我可以解決該代碼。我將它添加到mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);在onCreate下的 ? – John 2014-09-23 04:42:27

+1

不,我要做的是讓我的活動實現LocationListener,並使LocationManager公開於類級別,然後只是在同一個類中完成工作,並在解析工作之後將onUpdates放在onLocationChanged中 – JRowan 2014-09-23 04:49:59

+0

嗨,我注意到的一個問題removeUpdates是它只抓取一次,這是我想要的。我的意思是,每次有人啓動應用程序時,它都會記錄一次新的位置,並且可以說每30分鐘更新一次位置(因爲可以說這個人永遠不會關閉應用程序並且正在進行公路旅行)反對持續。 – John 2014-09-23 15:34:27

1

後再說什麼@JRowan說,你也可以使用:

String provider = LocationManager.getBestProvider(new Criteria(), true); 
Location myLocation = LocationManager.getLastKnowLocation(provider); 

如果你的位置定時不必須是精確的,也就是說,如果你能承受的位置數據是有點老了。

Method link

+0

感謝您的及時回覆。隨着添加建議的線我需要刪除任何線? – John 2014-09-23 04:41:08

+0

使用此代碼,你並不需要改變任何東西。不是在'onLocationReceived'中獲取位置,而是使用'myLocation'。對於@ JRowan的回答,你需要在完成接收信息後調用'removeUpdates',並且安全地存儲所有內容。 – nem035 2014-09-23 04:45:28

+0

嗨,我注意到removeUpdates的一個問題是它只抓取一次位置,這是我想要的。我的意思是,每次有人啓動應用程序時,它都會記錄一次新的位置,並且可以說每30分鐘更新一次位置(因爲可以說這個人永遠不會關閉應用程序並且正在進行公路旅行)反對持續。 – John 2014-09-23 15:34:06