0

我使用GoogleApiClient獲得在服務類的當前位置,並同時開始我想要得到的移動位置服務的狀態,如果位置服務是禁用,然後我想顯示彈出式啓用它。Android的檢查位置服務啓用如果使用GoogleApiClient

那麼,如何檢查位置服務的使用GoogleApiClient

這裏的狀態就是我的服務類

public class MyService extends Service implements LocationListener, 
GoogleApiClient.ConnectionCallbacks, 
GoogleApiClient.OnConnectionFailedListener{ 


    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 




    private static final long INTERVAL = 1000 * 300; 
    private static final long FASTEST_INTERVAL = 1000 * 200; 

    public MyService() { 
     super(); 

    } 


    @Override 
    public void onCreate() { 
     super.onCreate(); 
     //initialize location request 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(INTERVAL); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

     //initialize GooleApiClient 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addApi(LocationServices.API) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .build(); 

       //code to find nearby using TimerTask 
       Timer timer = new Timer(); 
       TimerTaskFindNearby findNearby = new TimerTaskFindNearby(getApplicationContext()); 
       timer.scheduleAtFixedRate(findNearby,1000,AppDataHandler.TASK_TIME); 


    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     mGoogleApiClient.connect(); 
     Log.d("Service","onstartcommand"); 
     return Service.START_STICKY; 
    } 

    @Override 
    public void onConnected(Bundle arg0) { 


     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
     else{ 
      onLocationChanged(location); 
     } 

    } 



    @Override 
    public void onLocationChanged(Location location) { 


     AppDataHandler.myLocation.setLatitude(String.valueOf(location.getLatitude())+""); 
     AppDataHandler.myLocation.setLongitude(String.valueOf(location.getLongitude())+""); 

     //store on shared pref 
     SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("FriendFinderSharedPref", Context.MODE_PRIVATE); 

     LocationBean myLocation = new LocationBean(); 
     myLocation.setLatitude(String.valueOf(location.getLatitude())+""); 
     myLocation.setLongitude(String.valueOf(location.getLongitude())+""); 
     myLocation.setMobile(sharedPreferences.getString("myMobile", "noData")); 

     //save to shared pref to make accessible from TimerTask 
     Gson gson = new Gson(); 
     Editor editor = sharedPreferences.edit(); 
     editor.putString("myLocation", gson.toJson(myLocation).toString()); 
     editor.commit(); 

     //store location on server by using volley String request 
     String url = baseURL+"userdata/savemylocation"; 


      //inform to activity 
      sendBroadcast(); 
    } 


    //send broadcast from activity to all receivers listening to the action "ACTION_STRING_ACTIVITY" 
    private void sendBroadcast() { 
     final Intent new_intent = new Intent(); 
     new_intent.setAction(AppDataHandler.ACTIVITY_RECEIVER_ACTION); 

     sendBroadcast(new_intent); 


    } 

    @Override 
    public void onDestroy() { 
     Log.d("service", "destroy"); 
     super.onDestroy(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult arg0) {} 
    @Override 
    public void onConnectionSuspended(int arg0) {} 


    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 
    } 

} 
+0

檢查谷歌播放服務是可用還是沒有看到這個鏈接http://stackoverflow.com/questions/22493465/check-if-correct-google-play-service-available-unfortunately-application-has-s – Hanuman

+0

該帖子未顯示正確的方式 – Gaurav

+0

所以你想要的郵政編碼是什麼你試過 – Hanuman

回答

1
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
      .addLocationRequest(mLocationRequest); 
    builder.setAlwaysShow(true); 

    result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(LocationSettingsResult result) { 
      final Status status = result.getStatus(); 
      //final LocationSettingsStates state = result.getLocationSettingsStates(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        // All location settings are satisfied. The client can initialize location 
        // requests here. 
        //... 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        // Location settings are not satisfied. But could be fixed by showing the user 
        // a dialog. 
        try { 
         // Show the dialog by calling startResolutionForResult(), 
         // and check the result in onActivityResult(). 
         status.startResolutionForResult(
           getActivity(), 
           REQUEST_LOCATION); 
        } catch (IntentSender.SendIntentException e) { 
         // Ignore the error. 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        // Location settings are not satisfied. However, we have no way to fix the 
        // settings so we won't show the dialog. 
        //... 
        break; 
      } 
     } 
    }); 

} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    Log.d("onActivityResult()", Integer.toString(resultCode)); 

    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); 
    switch (requestCode) 
    { 
     case REQUEST_LOCATION: 
      switch (resultCode) 
      { 
       case Activity.RESULT_OK: 
       { 
        // All required changes were successfully made 
        Toast.makeText(getActivity(), "Location enabled by user!", Toast.LENGTH_LONG).show(); 
        break; 
       } 
       case Activity.RESULT_CANCELED: 
       { 
        // The user was asked to change settings, but chose not to 
        Toast.makeText(getActivity(), "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show(); 
        break; 
       } 
       default: 
       { 
        break; 
       } 
      } 
      break; 
    } 
} 

這將顯示一個對話框if位置是OFF

0

我知道這是爲時已晚,但你可以使用此功能:

private boolean checkPlayServices() { 
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); 
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (apiAvailability.isUserResolvableError(resultCode)) { 
      apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) 
        .show(); 
     } else 
      finish(); 

     return false; 
    } 
    return true; 
} 
+0

謝謝@Fadwa_Imh但我想要的是檢查天氣的位置是開還是關,而不是播放服務 – Gaurav

+0

所以,你可以使用** ** LocationSettingsRequest,我會後的代碼段用於此。 –

相關問題