2016-06-28 5 views
-7

Ich versuche, Werte zwischen einem Service A zu einem Fragment B zu teilen. Wie kann ich tun? Ich benutzte die Funktion Shared Preferences aber ohne Ergebnisse.Wie kann ich Werte zwischen einem Service und einem Fragment in Android teilen?

Dies ist der Dienst A:

public class GPSManager extends Service implements LocationListener { 

public Context context; 
boolean isGPSEnabled = false; 
boolean isNetworkEnabled = false; 
Location location; 
double latitude; 
double longitude; 
double myLat; 
double myLong; 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
private static final long MIN_TIME_BW_UPDATES = 60000; 
protected LocationManager locationManager; 

SharedPreferences sharedPreferences_LAT_LONG = getApplicationContext().getSharedPreferences("Pref.PREFS_LAT_LONG", 0); 

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE); 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (isNetworkEnabled) 
     { 
      getGeoData(LocationManager.NETWORK_PROVIDER); 
     } 
     else if (isGPSEnabled && location == null) 
     { 
      getGeoData(LocationManager.GPS_PROVIDER); 
     } 
     else 
     { 
      //no gps and network 
     } 

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

private void getGeoData(String provider) 
{ 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    locationManager.requestLocationUpdates(provider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
    if (locationManager != null) 
    { 
     location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (location != null) 
     { 
      latitude = location.getLatitude(); 
      myLat = latitude; 
      longitude = location.getLongitude(); 
      myLong = longitude; 

      /*SharedPreferences.Editor editor_LAT_LONG = sharedPreferences_LAT_LONG.edit(); 
      editor_LAT_LONG.putFloat(String.valueOf(myLat), 0); 
      editor_LAT_LONG.putFloat(String.valueOf(myLong), 0); 
      editor_LAT_LONG.apply();*/ 

     } 
    } 
} 

Dies ist das Fragment B:

public class MapFragment extends Fragment { 

MapView mapView; 

private GoogleMap mMap; 

//LatLng Fabrinao_ViaBalbo = new LatLng(43.334546, 12.903811); 
LatLng Fabriano_ViaTerenzioMamiani = new LatLng(43.3355506, 12.9024512); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.map, container, false); 

    mapView = (MapView) rootView.findViewById(R.id.mapView); 
    mapView.onCreate(savedInstanceState); 

    mapView.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      Log.i("DEBUG", "onMapReady"); 
      mMap = googleMap; 
      //mMap.setMyLocationEnabled(true); 
      mMap.getUiSettings().setMapToolbarEnabled(false); //Toolbar Disattivata 

      /*mMap.addMarker(new MarkerOptions() 
        .position(My_Posizione) 
        .title("Wow!") 
        .snippet("Io sono qui"));*/ 

      mMap.addMarker(new MarkerOptions() 
        .position(Fabriano_ViaTerenzioMamiani) 
        .title("Fabriano - Via Terenzio Mamiani") 
        .snippet("Hello!") 
        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_logomarker))); 

      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Fabrinao_ViaBalbo, 15)); 
      //mMap.setMyLocationEnabled(true); 
     } 
    }); 

    return rootView; 
} 


@Override 
public void onResume() { 
    mapView.onResume(); 
    super.onResume(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 

Ich möchte den Wert von Latitude (myLat) und Länge speichern (myLong) vom Service A zum fra B so kann ich meine Position auf Karte zeigen.

Danke allen und Entschuldigung für mein schlechtes Englisch.

+5

Verwenden putExtra zu lesen. – Amy

+1

* Dies ist die Aktivität A: * ... nein, es ist ein ** Service ** ... * Dies ist die Aktivität B: * Nein, es ist ein ** Fragment ** – Selvin

Antwort

1

Sie können die Bundle-Klasse und die put_extra-Daten abrufen und ablegen.

Beispiel: put_extra

Intent i = new Intent(); 
i.putExtra("name_of_extra", myParcelableObject); 

Daten auf Vorsatz

Bundle b = new Bundle(); 
b = getIntent().getExtras(); 
Object myParcelableObject = b.getString("name_of_extra"); 
0

Sie können entweder:

intent.putExtra() 

oder Sie

SharedPreferences

intent.putExtra() verwendet werden können, wenn Sie eine neue Aktivität, wie das Starten dies:

Intent intent = new Intent(activityOne, activityTwo); 
intent.putExtra("TAG",VALUE); 
startActivity(intent); 

hier kann der Wert ein String sein, int, Bundle .... mehr über diese here

lesen und Sie können diesen Wert in der neuen Absicht retreive unter Verwendung:

getIntent().getStringExtra(); // if your data was a String 
0

Use-Schnittstelle oder Rückruf zum Senden von Daten. Zwischen Aktivität zu Aktivität oder Aktivität zu Fragment.

Read here

0

in Aktivität A

Intent yourInent = new Intent (thisActivity.this, nextActivity.class); Bündel b = neues Bündel(); b.putDouble ("key", doubleVal); IhrIntent.putExtras (b); startActivity (deinIntent);

in Tätigkeit B

Bundle b = getIntent() getExtras (.); double result = b.getDouble ("Schlüssel");

Verwandte Themen