Created
October 1, 2015 20:14
-
-
Save px-amaac/a248d2d27b22adaefbd2 to your computer and use it in GitHub Desktop.
activity that uses the location client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SplashActivity extends BaseActivity implements LocationListener { | |
// Splash screen timer | |
private static int SPLASH_TIME_OUT = 1000; | |
private LocationClient mLocationClient; | |
private Long mStartTime; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_splash); | |
Global.ScreenWidth = mDisplayMetrics.widthPixels; | |
Global.ScreenHeight = mDisplayMetrics.heightPixels; | |
mStartTime = System.currentTimeMillis(); | |
mLocationClient = new LocationClient(this, this); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (mLocationClient != null) { | |
mLocationClient.cleanUpLocationRequest(); | |
} | |
} | |
@Override | |
public void onLocationChanged(final Location location) { | |
long currentTimeDiff = mStartTime - System.currentTimeMillis(); | |
long splashTimeout = currentTimeDiff < SPLASH_TIME_OUT && currentTimeDiff > 0 ? currentTimeDiff : 0; | |
final Context self = SplashActivity.this; | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
saveLocation(location); | |
Global.loadPreferences(SplashActivity.this); | |
if (Global.IsSigned == true) { | |
Intent i = new Intent(SplashActivity.this, HomeActivity.class); | |
self.startActivity(i); | |
} else { | |
Intent i = new Intent(SplashActivity.this, SignupActivity.class); | |
self.startActivity(i); | |
} | |
// close this activity | |
finish(); | |
} | |
}, splashTimeout); | |
} | |
private void saveLocation(Location location) { | |
Address address = getAddressFromLocation(location); | |
if(address == null) | |
return; | |
final String strCity=address.getLocality(); | |
final String strZipCode=address.getPostalCode(); | |
HashMap<String, Object> request=new HashMap<String, Object>(); | |
request.put("type","update_city"); | |
request.put("userId",Global.UserInfo[Global.USER_ID]); | |
request.put("city",strCity); | |
if(!Global.isEmptyString(strZipCode)) | |
request.put("zipcode",strZipCode); | |
ParseCloud.callFunctionInBackground("update_user", request, new FunctionCallback<String>() { | |
public void done(String result, ParseException e) { | |
if (e == null) { | |
Global.UserInfo[Global.USER_CITY] = strCity; | |
Global.UserInfo[Global.USER_ZIPCODE] = strZipCode; | |
Global.savePreferences(null); | |
} else { | |
Log.d("", e.getMessage()); | |
} | |
Global.hideLoadingProgress(); | |
} | |
}); | |
} | |
private Address getAddressFromLocation(Location location) { | |
Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); | |
try { | |
List<Address> address = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); | |
if (address.size() > 0) { | |
int maxLines = address.get(0).getMaxAddressLineIndex(); | |
if (maxLines > 0) { | |
final String strCity=address.get(0).getLocality(); | |
if(Global.isEmptyString(strCity)) | |
return null; | |
return address.get(0); | |
} | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment