2017-10-02 2 views
0

私は場所の許可を得てサービスを提供しましたが、地図の断片を追加することなくサービスから私の場所を取得する方法はわかりません。 私の場所を取得し、TextViewにTextを設定する必要があります。 //このアクティビティでフラグメントを取得する必要はありません TextViewでpostメソッドを使用する必要がありますか?どのようにサービスからロケーションを取得できますか? 私を助けてください!おかげアンドロイドのアクティビティでマップフラグメントを追加せずに自分の位置を取得するにはどうすればよいですか?

これは私のMainActivity.javaではPermissionActivity

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

} 

@Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
    switch (compoundButton.getId()) { 
     case R.id.locationSwitch: 
      Constants.checkFineLocationPermission(MainActivity.this); 
      if(compoundButton.isChecked()){ 
       Intent intent = new Intent(MainActivity.this, LocationService.class); 
       startService(intent); 
      }else { 
       Intent intent = new Intent(MainActivity.this, LocationService.class); 
       stopService(intent); 
      } 
      break; 
    } 

} 

を拡張し、これがPermissionActivityです。

public void onRequestPermissionsResult(int requestCode, String permissions[],int[] grantResults){ 
     switch (requestCode){ 
      case MY_PERMISSIONS_REQUEST_FINE_LOCATION:{ 
       if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){ 
        Intent intent = new Intent(getApplicationContext(),getClass()); 
        startActivity(intent); 
        finish(); 
       }else{ 
        // Log.d("Permission always denyed"); 
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) 
          .setData(Uri.parse("package:"+ getApplicationContext().getPackageName())); 
        startActivity(intent); 
       } 
       return; 
      } 

     } 
    } 

と、これはサービス

private class LocationListener implements android.location.LocationListener 
{ 
    Location mLastLocation; 

    public LocationListener(String provider) 
    { 
     Log.e(TAG, "LocationListener " + provider); 
     mLastLocation = new Location(provider); 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     Log.e(TAG, "onLocationChanged: " + location); 
     mLastLocation.set(location); 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
     Log.e(TAG, "onProviderDisabled: " + provider); 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
     Log.e(TAG, "onProviderEnabled: " + provider); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     Log.e(TAG, "onStatusChanged: " + provider); 
    } 
} 

LocationListener[] mLocationListeners = new LocationListener[] { 
     new LocationListener(LocationManager.GPS_PROVIDER), 
     new LocationListener(LocationManager.NETWORK_PROVIDER) 
}; 

@Override 
public IBinder onBind(Intent arg0) 
{ 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    Log.e(TAG, "onStartCommand"); 
    super.onStartCommand(intent, flags, startId); 
    return START_STICKY; 
} 

@Override 
public void onCreate() 
{ 
    Log.e(TAG, "onCreate"); 
    initializeLocationManager(); 
    try { 
     mLocationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
       mLocationListeners[1]); 
    } catch (java.lang.SecurityException ex) { 
     Log.i(TAG, "fail to request location update, ignore", ex); 
    } catch (IllegalArgumentException ex) { 
     Log.d(TAG, "network provider does not exist, " + ex.getMessage()); 
    } 
    try { 
     mLocationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
       mLocationListeners[0]); 
    } catch (java.lang.SecurityException ex) { 
     Log.i(TAG, "fail to request location update, ignore", ex); 
    } catch (IllegalArgumentException ex) { 
     Log.d(TAG, "gps provider does not exist " + ex.getMessage()); 
    } 
} 

@Override 
public void onDestroy() 
{ 
    Log.e(TAG, "onDestroy"); 
    super.onDestroy(); 
    if (mLocationManager != null) { 
     for (int i = 0; i < mLocationListeners.length; i++) { 
      try { 
       mLocationManager.removeUpdates(mLocationListeners[i]); 
      } catch (Exception ex) { 
       Log.i(TAG, "fail to remove location listners, ignore", ex); 
      } 
     } 
    } 
} 

private void initializeLocationManager() { 
    Log.e(TAG, "initializeLocationManager"); 
    if (mLocationManager == null) { 
     mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
    } 
} 

}

+0

マップは必要ありません。場所を取得するためにLocationManagerを使用するだけですか? –

+0

私は、LocationManagerのインスタンスが私のサービス上に存在し、そのインスタンスにどのようにアクセスできるのかわかりません。ゲッターメソッドでそのインスタンスを取得できますか? –

+0

とそのクラスではどのような方法が必要ですか?私は都市、通りなどの詳細な場所を取得する必要がある –

答えて

0

を呼び出すことにより、このクラス を使用しようですdロケーションサービス。それは最も正確な位置を提供します。チュートリアルherehereをご覧ください。だから基本的には、このサービスをサービスに入れてください。

+0

それはあなたのURLと私はそれが存在してもわからなかった:) –

+0

@ J.hoは助けてうれしい:) –

0

あなたがGoogleのヒューズを使用する必要があります

private void getCurrentLocation() { 
    GPSTracker gps = new GPSTracker(activity); 

    // check if GPS enabled 
    if (gps.canGetLocation()) { 

     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 

    // Do What you want to do 
    } 
} 

public class GPSTracker extends Service implements LocationListener { 

private final Context mContext; 

// flag for GPS status 
boolean isGPSEnabled = false; 

// flag for network status 
boolean isNetworkEnabled = false; 

// flag for GPS status 
boolean canGetLocation = false; 

Location location; // location 
double latitude; // latitude 
double longitude; // longitude 

// The minimum distance to change Updates in meters 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

// The minimum time between updates in milliseconds 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

// Declaring a Location Manager 
protected LocationManager locationManager; 

public GPSTracker(Context context) { 
    this.mContext = context; 
    getLocation(); 
} 

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      // First get location from Network Provider 
      if (isNetworkEnabled) { 
       try { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.e("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } catch (SecurityException e) { 
        Log.e("SecurityException", " 1 " + e.toString()); 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        try { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.e("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } catch (SecurityException e) { 
         Log.e("SecurityException", " 2 " + e.toString()); 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 


public void stopUsingGPS() { 
    if (locationManager != null) { 
     try { 
      locationManager.removeUpdates(GPSTracker.this); 
     } catch (SecurityException e) { 
      Log.e("SecurityException", " 3 " + e.toString()); 
     } 

    } 
} 


public double getLatitude() { 
    if (location != null) { 
     latitude = location.getLatitude(); 
    } 

    // return latitude 
    return latitude; 
} 

public double getLongitude() { 
    if (location != null) { 
     longitude = location.getLongitude(); 
    } 

    // return longitude 
    return longitude; 
} 


public boolean canGetLocation() { 
    return this.canGetLocation; 
} 


public void showSettingsAlert(final Activity activity) { 

    new MaterialDialog.Builder(activity) 
      .title(R.string.EnableGPS_st) 
      .content(R.string.GPSSittings_st) 
      .contentColor(UiConstants.Colors.colorBlack) 
      .titleColor(UiConstants.Colors.colorBlack) 
      .positiveColor(UiConstants.Colors.colorBlack) 
      .negativeColor(UiConstants.Colors.colorBlack) 
      .positiveText(R.string.Settings_st) 
      .negativeText(R.string.cancel_st) 
      .callback(new MaterialDialog.ButtonCallback() { 
       @Override 
       public void onPositive(MaterialDialog dialog) { 
        dialog.dismiss(); 

        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        activity.startActivity(intent); 
       } 
       @Override 
       public void onNegative(MaterialDialog dialog) { 
        super.onNegative(dialog); 
        dialog.dismiss(); 
       } 
      }).cancelable(false) 
      .show(); 
} 

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onProviderDisabled(String provider) { 
} 

@Override 
public void onProviderEnabled(String provider) { 
} 

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

@Override 
public IBinder onBind(Intent arg0) { 
    return null; 
} 

}

関連する問題