2016-10-03 14 views
0

我有2個不同的類,第一類Tracking.java和第二類ReportingService.java。如何將ReportingService.java上的位置地址傳遞給Tracking.java?如何在類之間傳遞接收的數據Android

private void doLogout(){ 
    Log.i(TAG, "loginOnClick: "); 
    //ReportingService rs = new ReportingService(); 
    //rs.sendUpdateLocation(boolean isUpdate, Location); 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(NetHelper.getDomainAddress(this)) 
      .addConverterFactory(ScalarsConverterFactory.create()) 
      .build(); 

    ToyotaService toyotaService = retrofit.create(ToyotaService.class); 

    // caller 
    Call<ResponseBody> caller = toyotaService.logout("0,0", 
      AppConfig.getUserName(this), 
      "null"); 
    // async task 
    caller.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      try { 
       Log.i(TAG, "onResponse: "+response.body().string()); 
      }catch (IOException e){} 

     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      Log.e(TAG, "onFailure: ", t); 
     } 
    }); 

    AppConfig.saveLoginStatus(this, AppConfig.LOGOUT); 
    AppConfig.storeAccount(this, "", ""); 

    Intent intent = new Intent(this, Main2Activity.class); 
    startActivity(intent); 
    finish(); 
} 

此代碼爲位置的地址

Call<ResponseBody> caller = toyotaService.logout("0,0", 
      AppConfig.getUserName(this), 
      "null"); 

這是從GoogleMap的代碼獲取經度,緯度和位置的地址的類ReportingService.java位置

private void sendUpdateLocation(boolean isUpdate, Location location) { 
    Log.i(TAG, "onLocationChanged "+location.getLongitude()); 

    Geocoder geocoder; 
    List<Address> addresses; 
    geocoder = new Geocoder(this, Locale.getDefault()); 
    String street = "Unknown"; 
    try { 
     addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
     if (addresses != null) { 
      String address = addresses.get(0).getAddressLine(0); 
      String city = addresses.get(0).getLocality(); 
      String state = addresses.get(0).getAdminArea(); 
      String country = addresses.get(0).getCountryName(); 
      String postalCode = addresses.get(0).getPostalCode(); 
      String knowName = addresses.get(0).getFeatureName(); 
      street = address + " " + city + " " + state + " " + country + " " + postalCode + " " + knowName; 
      Log.i(TAG, "street "+street); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    if (isUpdate) 
     NetHelper.report(this, AppConfig.getUserName(this), location.getLatitude(), 
       location.getLongitude(), street, new PostWebTask.HttpConnectionEvent() { 
      @Override 
      public void preEvent() { 

      } 

      @Override 
      public void postEvent(String... result) { 
       try { 
        int nextUpdate = NetHelper.getNextUpdateSchedule(result[0]); // in second 
        Log.i(TAG, "next is in " + nextUpdate + " seconds"); 
        if (nextUpdate > 60) { 
         dismissNotification(); 
         isRunning = false; 
        } else if (!isRunning){ 
         showNotification(); 
         isRunning = true; 
        } 
        handler.postDelayed(location_updater, nextUpdate * 1000 /*millisecond*/); 
       }catch (JSONException e){ 
        Log.i(TAG, "postEvent error update"); 
        e.printStackTrace(); 
        handler.postDelayed(location_updater, getResources().getInteger(R.integer.interval) * 1000 /*millisecond*/); 
       } 
      } 
     }); 
    else 
     NetHelper.logout(this, AppConfig.getUserName(this), location.getLatitude(), 
       location.getLongitude(), street, new PostWebTask.HttpConnectionEvent() { 
      @Override 
      public void preEvent() { 

      } 

      @Override 
      public void postEvent(String... result) { 
       Log.i(TAG, "postEvent logout "+result); 
      } 
     }); 
} 

謝謝

回答

0

使用this庫,並按照其中提供的示例。 它可以傳遞你想要的任何東西。

0

我想只是使用一個接口將解決您的問題。 僞代碼

ReportingException.java

添加此

public interface myLocationListner{ 
onRecievedLocation(String location); 
} 

private myLocationListner mylocation; 

//下面添加行,你在Tracking.java 有你的街道地址

mylocation.onRecievedLocation(street); 

然後實現myLocationListner去:)

0

你可以使用一個意圖:

意圖將觸發第二個接收器和將數據傳遞到

如果廣播接收器:

Intent intent = new Intent(); 
intent.setAction("com.example.2ndReceiverFilter"); 
intent.putExtra("key" ,); //put the data you want to pass on 
getApplicationContext().sendBroadcast(intent); 

如果服務:

Intent intent = new Intent();` 
intent.putExtra("key" , value); //put the data you want to pass on 
startService(ReportingService.this , Tracking.class); 

在Tracking.java,檢索您傳遞的數據: inside onReceive,將此代碼首先放入

intent.getExtras().getString("key");//if int use getInt("key")