2017-03-18 49 views
-2

我有以下腳本中使用GoogleAPI得到位置。在我的應用程序上工作是基於位置。當此活動打開後,感謝使用Google API獲取的位置稱爲經度和緯度。如何解決關閉位置

當MainActivity被打開,如果定位設備設置中開啓。(Android智能手機).Location知識是作爲一個成功的方式和MainActivity不會發生任何問題獲得。相反,當位置關閉 我的Android設備上,而活動開始時,我有錯誤,應用程序關閉了那個叫「不幸的是該應用程序已停止」的消息。

我該如何解決這個問題?而活動正在打開如何控制位置是打開還是關閉。

在此先感謝整個幫助。

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 
    , LocationListener , DialogInterface.OnClickListener { 
public static final String KEY_LOC_ADD_URL = "http://xxxxxxxx/android_api/insertlocation.php"; 
public static final String KEY_LATITUDE = "enlem"; 
public static final String KEY_LONGITUDE = "boylam"; 
public static final String KEY_ID="id"; 

private GoogleApiClient googleApiClient; 
private LocationRequest locationRequest; 
private Location mevcutKonum; 

private String[] seconds={"1","3","5","7","10"}; 
private String sonGuncellemeZamani; 
private String employee_id; 
private Long requestTime; 

private TextView enlemTextView; 
private TextView boylamTextView; 
private TextView sonGuncellemeTextView; 
private TextView employee_name; 



private AlertDialog dialogSelectInternal; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    enlemTextView = (TextView) findViewById(R.id.enlem); 
    boylamTextView = (TextView) findViewById(R.id.boylam); 
    sonGuncellemeTextView = (TextView) findViewById(R.id.guncellemezamani); 
    sonGuncellemeZamani = ""; 
    employee_name= (TextView) findViewById(R.id.userName); 

    Intent intent = getIntent(); 
    employee_name.setText("Welcome User " +  intent.getStringExtra(Login_Activity.KEY_USERNAME)); 
    employee_id=intent.getStringExtra(Login_Activity.KEY_ID); 


    buildGoogleApiClient(); 
    selectRequestTimeDialog(); 
    createLocationRequest(); 

} 





protected void buildGoogleApiClient() { 
    googleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 

protected void createLocationRequest() { 
    locationRequest = LocationRequest.create() 
      .setInterval(3000) 
      .setFastestInterval(1000) 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
} 

protected void startLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(
      googleApiClient, locationRequest, this); 
} 

protected void stopLocationUpdates() { 
    LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); 
} 








private void updateUI() { 


    if (mevcutKonum != null) { 
     enlemTextView.setText(String.valueOf(mevcutKonum.getLatitude())); 
     boylamTextView.setText(String.valueOf(mevcutKonum.getLongitude())); 
     sonGuncellemeTextView.setText(sonGuncellemeZamani); 
    } 
} 


@Override 
protected void onResume() { 
    super.onResume(); 
    googleApiClient.connect(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (googleApiClient.isConnected()) { 
     stopLocationUpdates(); 
    } 

    googleApiClient.disconnect(); 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 




@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (mevcutKonum == null) { 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      return; 
     } 
     mevcutKonum = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
     sonGuncellemeZamani = DateFormat.getTimeInstance().format(new Date()); 

     try { 
      addLocationToUser(String.valueOf(mevcutKonum.getLatitude()),String.valueOf(mevcutKonum.getLongitude()),employee_id); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     updateUI(); 


    } 

    startLocationUpdates(); 

} 



@Override 
public void onConnectionSuspended(int i) { 

} 


@Override 
public void onLocationChanged(Location location) { 
    mevcutKonum = location; 
    sonGuncellemeZamani = DateFormat.getTimeInstance().format(new Date()); 
    updateUI(); 


} 


private void addLocationToUser(final String latitude, final String longitude,final String id) throws JSONException { 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, KEY_LOC_ADD_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<>(); 
       params.put(KEY_LATITUDE,latitude); 
       params.put(KEY_LONGITUDE, longitude); 
       params.put(KEY_ID,id); 

       return params; 
      } 

     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 


private void selectRequestTimeDialog(){ 

    AlertDialog.Builder builder=new AlertDialog.Builder(this); 
    builder.setTitle("Konumunuzu Güncellemek istediğiniz zaman aralığını seçiniz"); 
    builder.setItems(seconds,this); 
    builder.setNegativeButton("Cancel", null); 
    dialogSelectInternal=builder.create(); 
    dialogSelectInternal.show(); 
} 


@Override 
public void onClick(DialogInterface dialog, int which) { 


} 

}

上面的代碼時位置只打開

回答

0

在你onCreate()工作過,如果你的GPS功能,設備啓用,如果是在創建位置更新首先檢查,否則轉到位置設置

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      createLocationRequest(); 
     } else { 
      Toast.makeText(this, "Please enable your gps first.", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent, 1); 
     } 

在GPS - >高精度的選擇:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == 1) { 
     switch (requestCode) { 
      case 1: 
       createLocationRequest(); 
      break; 
     } 
    } 
} 

希望這有助於。

+0

謝謝你的幫助,但我不明白。我必須使用你的上面和下面的代碼中的每兩種方法嗎? @tahsinRupam – vahitdurmus

+0

第一個代碼段檢查,如果GPS是,如果是,那麼得到的位置或者去到您的設備的位置設置。第二個代碼,如果選擇HIGH ACCURACY,則執行,然後獲取位置。希望這會清除一些事情。 – tahsinRupam

+0

非常感謝您的關注。我會嘗試 – vahitdurmus

相關問題