2014-02-17 36 views
1

我有這個問題,我從我的應用程序使用GPS來獲取經度和緯度,當我想返回到家時圖標GPS始終顯示從我的移動設備的頂部,我如何可以返回主屏幕後關閉它Android如何在按回到主屏幕後關閉gps圖標?

enter image description here 我的代碼。

public class doctorlocation extends Activity implements LocationListener { 
    protected LocationManager locationManager; 
    protected LocationListener locationListener; 
    protected Context context; 
    String lat; 
    String provider; 
    protected double latitude,longitude; 
    protected boolean gps_enabled,network_enabled; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.doctorlocation); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    } 

     @Override 
    public void onLocationChanged(Location location) { 
    //txtLat = (TextView) findViewById(R.id.textview1); 

    //txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
    String str = "Latitude: "+location.getLatitude()+" \nLongitude: "+location.getLongitude(); 
    Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show(); 

    latitude=location.getLatitude(); 

    longitude=location.getLongitude(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    Log.d("Latitude","disable"); 

     Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    Log.d("Latitude","enable"); 

    Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    Log.d("Latitude","status"); 
    } 

     } 

回答

3

ActivityonPause()上使用此代碼。

@Override 
    protected void onPause() { 
    Log.i("onPause", "inside onPause"); 
    super.onPause(); 
    locationManager.removeUpdates(myLocationListener);//locationManager.removeUpdates(this) ; 
    locationManager = null; 
    } 
+0

謝謝你我已經添加你的代碼時,按下恢復按鈕回家我不幸的是你的應用程序已停止。 – Gulan

+0

'locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);'在'onResume'上使用它。 – ryderz8

1

啓用和禁用GPS在用戶手中。您可以向他展示一個對話框,通知他關於禁用GPS。在這種情況下,在對話框中保留兩個按鈕 - 一個用於「設置」,另一個用於「確定」或「取消」。

public static void promptForGPS(
     final Activity activity) 
    { 
     final AlertDialog.Builder builder = 
      new AlertDialog.Builder(activity); 
     final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS; 
     final String message = "Disable GPS....Message here" 
      + " Click OK to go to" 
      + " location services settings to let you do so."; 

     builder.setMessage(message) 
      .setPositiveButton("OK", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface d, int id) { 
         activity.startActivity(new Intent(action)); 
         d.dismiss(); 
        } 
      }) 
      .setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface d, int id) { 
         d.cancel(); 
        } 
      }); 
     builder.create().show(); 
    }  
+0

謝謝你的回答,但現在我有一個錯誤(ACTION_LOCATION_SOURCE_SETTINGS無法解析或不是字段) – Gulan

+0

檢查哪些「設置」已導入... – user2450263

+1

http://developer.android.com/reference/ android/provider/Settings.html#ACTION_LOCATION_SOURCE_SETTINGS – user2450263

0

我發現,最好的是

onResume() 
{ 
    // Request for GPS Location 
    //location.requestLocationUpdates() 
} 

onPause() 
{ 
    // Remove location 
    // location.removeUpdates() 
} 

通過電話removeUpdates()方法,只要您的活動是不是在forground將停止GPS。這樣您就可以停止GPS以及停止耗盡電池。

0

您需要在您的onPause()方法使用

locationManager.removeUpdates(this); 

告訴的LocationManager,你不再需要位置更新時,您的應用程序進入後臺。

你也應該將你的呼叫

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

到的onResume();所以一旦你重新進入你的應用程序,位置更新將恢復。