Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. package com.hfad.spaceappswater;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.location.Address;
  6. import android.location.Geocoder;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.net.wifi.p2p.WifiP2pManager;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.*;
  15. import android.widget.*;
  16.  
  17. import java.io.IOException;
  18. import java.util.List;
  19. import java.util.Locale;
  20.  
  21. public class MainActivity extends AppCompatActivity{
  22.  
  23. private Button location_button = null;
  24. private TextView location_view = null;
  25. private Button water_button = null;
  26. private TextView water_view = null;
  27. private Spinner water_spinner = null;
  28.  
  29. private String latitude = "";
  30. private String longitude = "";
  31.  
  32. private int ltd;
  33. private int lng;
  34.  
  35. private static final String TAG = "MainActivity";
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_main);
  41.  
  42. location_button = (Button)findViewById(R.id.location_button);
  43. water_button = (Button)findViewById(R.id.water_button);
  44. location_view = (TextView)findViewById(R.id.location_view);
  45. }
  46.  
  47. private class LocListener implements LocationListener {
  48. //String elocation = "";
  49.  
  50. @Override
  51. public void onLocationChanged(Location loc) {
  52. String longitude = "Longitude: " + loc.getLongitude();
  53. double lng = loc.getLongitude();
  54. Log.v(TAG, longitude);
  55. double ltd = loc.getLatitude();
  56. String latitude = "Latitude: " + loc.getLatitude();
  57. Log.v(TAG, latitude);
  58.  
  59. /*------- To get city name from coordinates -------- */
  60. String cityName = null;
  61. Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
  62. List<Address> addresses;
  63. try {
  64. addresses = gcd.getFromLocation(loc.getLatitude(),
  65. loc.getLongitude(), 1);
  66. if (addresses.size() > 0) {
  67. System.out.println(addresses.get(0).getLocality());
  68. cityName = addresses.get(0).getLocality();
  69. }
  70. }
  71. catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
  75. + cityName;
  76. location_view.setText(s);
  77.  
  78. }
  79.  
  80. @Override
  81. public void onProviderDisabled(String provider) {}
  82.  
  83. @Override
  84. public void onProviderEnabled(String provider) {}
  85.  
  86. @Override
  87. public void onStatusChanged(String provider, int status, Bundle extras) {}
  88. }
  89.  
  90. public void locationClick(View view){
  91. String text = "Checking Location...";
  92. int duration = Toast.LENGTH_LONG;
  93.  
  94. Toast toast = Toast.makeText(this, text, duration);
  95. toast.show();
  96.  
  97. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  98. LocationListener locationListener = new LocListener();
  99. try{
  100. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener);}
  101. catch (SecurityException e){
  102. Log.d(TAG, "Access Denied! Please contact our operator");
  103. }
  104. }
  105.  
  106. public void waterClick(View view){
  107. if((latitude.equals(""))||(longitude.equals(""))){
  108. String text = "Error!!! Location not Checked!!!";
  109. int duration = Toast.LENGTH_SHORT;
  110.  
  111. Toast toast = Toast.makeText(this, text, duration);
  112. }
  113. else{
  114. Intent intent = new Intent(MainActivity.this, MapActivity.class);
  115. intent.putExtra(MapActivity.EXTRA_LATITUDE, ltd);
  116. intent.putExtra(MapActivity.EXTRA_LONGITUDE, lng);
  117. startActivity(intent);
  118. }
  119. }
  120.  
  121. @Override
  122. protected void onSaveInstanceState(Bundle savedInstanceState){
  123.  
  124. }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement