我得到了同樣的問題,我嘗試用Devunwired的很好的答案來解決它,但我有一些麻煩。我找不到停止服務的方法,當我完成我的應用時,GPS模塊仍在運行。所以,我發現了另一種方式:
我寫了一個GPS.java類:
public class GPS {
private IGPSActivity main;
// Helper for GPS-Position
private LocationListener mlocListener;
private LocationManager mlocManager;
private boolean isRunning;
public GPS(IGPSActivity main) {
this.main = main;
// GPS Position
mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
// GPS Position END
this.isRunning = true;
}
public void stopGPS() {
if(isRunning) {
mlocManager.removeUpdates(mlocListener);
this.isRunning = false;
}
}
public void resumeGPS() {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
this.isRunning = true;
}
public boolean isRunning() {
return this.isRunning;
}
public class MyLocationListener implements LocationListener {
private final String TAG = MyLocationListener.class.getSimpleName();
@Override
public void onLocationChanged(Location loc) {
GPS.this.main.locationChanged(loc.getLongitude(), loc.getLatitude());
}
@Override
public void onProviderDisabled(String provider) {
GPS.this.main.displayGPSSettingsDialog();
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
這個類是在每一個需要它的GPS座標活動中使用。每一項活動必須實現以下接口(需要通信):
public interface IGPSActivity {
public void locationChanged(double longitude, double latitude);
public void displayGPSSettingsDialog();
}
現在我的主要活動看起來像這樣:
public class MainActivity extends Activity implements IGPSActivity {
private GPS gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gps = new GPS(this);
}
@Override
protected void onResume() {
if(!gps.isRunning()) gps.resumeGPS();
super.onResume();
}
@Override
protected void onStop() {
gps.stopGPS();
super.onStop();
}
public void locationChanged(double longitude, double latitude) {
Log.d(TAG, "Main-Longitude: " + longitude);
Log.d(TAG, "Main-Latitude: " + latitude);
}
@Override
public void displayGPSSettingsDialog() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
,第二個這樣的:
public class TEST4GPS extends Activity implements IGPSActivity{
private GPS gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.gps = new GPS(this);
}
@Override
public void locationChanged(double longitude, double latitude) {
Log.d("TEST", "Test-Longitude: " + longitude);
Log.d("TEST", "Test-Latitude: " + latitude);
}
@Override
protected void onResume() {
if(!gps.isRunning()) gps.resumeGPS();
super. onResume();
}
@Override
protected void onStop() {
gps.stopGPS();
super.onStop();
}
@Override
public void displayGPSSettingsDialog() {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
它不像Devunwired的解決方案一樣美麗,但它適用於我。 CYA
我一直在這條路上,但我不知道onStart和onStop重疊。這對我很有吸引力,我會嘗試一下,讓你知道我明天想出了什麼。謝謝! – d370urn3ur 2011-04-25 22:28:37
只是想知道,這將關閉屏幕如何工作?在屏幕打開/關閉的同時打開/關閉onPause()onResume()和UNBIND上的服務綁定以及在活動切換時工作是明智的嗎? – 2011-06-23 19:29:55
@Grantland有效的點,因爲活動仍然是前臺服務將生活。如果服務需要在這些情況下將自己調整回來,我會保持綁定相同,但是爲'Intent.ACTION_SCREEN_ON'和'Intent.ACTION_SCREEN_OFF'註冊一個'BroadcastReceiver'來關閉像GPS這樣重的組件。將綁定機制移入onPause()/ onResume()會導致服務在整個應用程序的使用過程中可能會停止/重新啓動,因爲這些回調不會重疊,從而使服務經常處於未綁定狀態。 – Devunwired 2011-09-08 18:48:27