GPS から現在の日付と時刻を取得していますが、現在の日付の 3 日前の日付が返されます。MyLocationListener クラス内 loc.getTime(); 現在の日付の3日前の日付を教えてくれます。私が使用しているコード
public class Location_gps extends Activity
{
Button btn;
LocationManager mlocManager;
LocationListener mlocListener;
Location loc;
SimpleDateFormat sdf;
Date d;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_gps);
btn = (Button)findViewById(R.id.btn);
/* Use the LocationManager class to obtain GPS locations */
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
d = new Date(loc.getTime());
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String Text = "My current location is: " +
"\nLatitude = " + loc.getLatitude() +
"\nLongitude = " + loc.getLongitude() +
"\nAccuracy = " + loc.getAccuracy() +
"\nAltitude = " + loc.getAltitude() +
"\nProvider = " + loc.getProvider() +
"\nTime = " + sdf.format(d);
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
});
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"\nLatitude = " + loc.getLatitude() +
"\nLongitude = " + loc.getLongitude() +
"\nAccuracy = " + loc.getAccuracy() +
"\nAltitude = " + loc.getAltitude() +
"\nTime = " + loc.getTime();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
GPS から現在の日付と時刻を取得する方法。私を助けてください。