2010-08-12 30 views

Trả lời

19

Nếu bạn muốn chụp vị trí khi nhấn nút, dưới đây là cách bạn thực hiện. Nếu người dùng không bật dịch vụ vị trí, điều này sẽ gửi họ tới menu cài đặt để bật dịch vụ đó.

Trước tiên, bạn phải thêm quyền "android.permission.ACCESS_COARSE_LOCATION" vào tệp kê khai của mình. Nếu bạn cần GPS (vị trí mạng là không đủ nhạy cảm), thêm sự cho phép "android.permission.ACCESS_FINE_LOCATION" thay vào đó, và thay đổi "Criteria.ACCURACY_COARSE" thành "Criteria.ACCURACY_FINE"

Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation); 
gpsButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // Start loction service 
     LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE); 

     Criteria locationCritera = new Criteria(); 
     locationCritera.setAccuracy(Criteria.ACCURACY_COARSE); 
     locationCritera.setAltitudeRequired(false); 
     locationCritera.setBearingRequired(false); 
     locationCritera.setCostAllowed(true); 
     locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT); 

     String providerName = locationManager.getBestProvider(locationCritera, true); 

     if (providerName != null && locationManager.isProviderEnabled(providerName)) { 
      // Provider is enabled 
      locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener); 
     } else { 
      // Provider not enabled, prompt user to enable it 
      Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show(); 
      Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      [OUTERCLASS].this.startActivity(myIntent); 
     } 
    } 
}); 

lớp bên ngoài của tôi có người nghe này được thiết lập

private final LocationListener locationListener = new LocationListener() { 

    @Override 
    public void onLocationChanged(Location location) { 
     [OUTERCLASS].this.gpsLocationReceived(location); 
    } 

    @Override 
    public void onProviderDisabled(String provider) {} 

    @Override 
    public void onProviderEnabled(String provider) {} 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 

}; 

Sau đó, bất cứ khi nào bạn muốn ngừng nghe, hãy gọi cho cuộc gọi này. Bạn nên thực hiện cuộc gọi này trong phương thức onStop của hoạt động của bạn.

LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
locationManager.removeUpdates(this.locationListener); 
+2

Tham số thứ hai trên getBestProvider (locationCritera, true) kiểm tra mà các nhà cung cấp là được bật, do đó, về mặt lý thuyết, bạn không cần phải kiểm tra xem nó có được bật lại hay không. – Eduardo

0

Để nhắc nhở người dùng để cho phép dịch vụ định vị, bạn nên sử dụng LocationRequest mới bao gồm trong dịch vụ Google Play, bạn có thể tìm thấy hướng dẫn đầy đủ trong LocationRequest

2

Sau khi đi qua rất nhiều câu trả lời trên Stack Overflow, Tôi thấy phương pháp này hoạt động hoàn hảo và thậm chí không cần nhiều mã.
Khai báo int GPSoff = 0 dưới dạng biến toàn cầu.
Bây giờ bất cứ nơi nào bạn cần phải kiểm tra cho tình trạng hiện tại của GPS và tái trực tiếp cho người sử dụng để biến GPS trên, sử dụng này:

try { 
       GPSoff = Settings.Secure.getInt(getContentResolver(),Settings.Secure.LOCATION_MODE); 
      } catch (Settings.SettingNotFoundException e) { 
       e.printStackTrace(); 
      } 
      if (GPSoff == 0) { 
       showMessageOKCancel("You need to turn Location On", 
         new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
           startActivity(onGPS); 
          } 
         }); 
      } 
+0

getContentResolver(), showMessageOKCancel & startActivity là gì? Tất cả chúng đều chưa được giải quyết – hfz

Các vấn đề liên quan