2017-09-08 6 views
0

Activity 클래스에서 JobService로 위도 및 경도 값을 가져 오려고합니다. 어떻게해야합니까? putExtras 등으로 인 텐트를 사용해 보았지만 (아래 코드를 보아라.), 제대로 만들 수 없었다.Activity에서 JobService로 데이터 전달

MainActivity.class

protected void createLocationRequest(Bundle bundle) { 

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() { 
     @Override 
     public void onLocationResult(final LocationResult locationResult) { 
      Log.i("onLocationResult", locationResult + ""); 
      latitude = locationResult.getLastLocation().getLatitude() + ""; 
      longitude = locationResult.getLastLocation().getLongitude() + ""; 
      Log.e("onLocationResult lat", latitude); 
      Log.e("onLocationResult Lon", longitude); 
      //I need to send latitude and longitude value to jobService? 
      //how to do that? 

     //tried using intent but didn't seem to work 
      //Intent mIntent = new Intent(); 
      //mIntent.putExtra("lat", latitude); 
      //mIntent.putExtra("lon", longitude); 
     } 
    }, null); 
} 

MyJobService 클래스

public class MyJobService extends JobService { 

    @Override 
    public boolean onStartJob(JobParameters jobParameters) { 
     //I need to get latitude and longitude value here from mainActivity 
     return true; 
    } 
} 

답변

0

JobInfo 객체를 생성하는 곳에서는 setExtras()를 사용하여 PersistableBundle of extras를 전달합니다.

ComponentName componentName = new ComponentName(context, MyJobService.class); 

PersistableBundle bundle = new PersistableBundle(); 
bundle.putLong("lat", lat); 
bundle.putLong("lon", lon); 

JobInfo jobInfo = new JobInfo.Builder(0, componentName) 
     .setExtras(bundle) 
     .build(); 

그런 다음 JobService에 당신은

@Override 
public boolean onStartJob(JobParameters params) { 
    params.getExtras().getLong("lat"); 
    params.getExtras().getLong("lon"); 
} 
로를 검색 할 수 있습니다
1

당신이 공유 환경 설정에서 이러한 데이터를 저장할 수 있으며, onStartJob() 메서드를 사용하면 거기에서 위도 긴 데이터에 액세스 할 수 있습니다 호출합니다.

또한 최신 위치의 긴 값에 따라 공유 환경 설정 값을 업데이트하고 서비스가 실행될 때 최신의 긴 시간을 가져올 수 있습니다.