Guest User

Untitled

a guest
Jan 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. TextView mensaje1;
  4. TextView mensaje2;
  5. TextView hora;
  6. String id;
  7. TextView uno;
  8.  
  9. ProgressDialog pDialog;
  10. JSONParser jsonParser = new JSONParser();
  11. String REGISTER_URL = "http://www.syan.cl/musicminguzzi.cl/gps/gps.php";
  12. String TAG_SUCCESS = "success";
  13. String TAG_MESSAGE = "message";
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. mensaje1 = (TextView) findViewById(R.id.mensaje_id);
  20. mensaje2 = (TextView) findViewById(R.id.mensaje_id2);
  21. uno = (TextView) findViewById(R.id.ide);
  22. hora = (TextView) findViewById(R.id.hour);
  23.  
  24. Bundle extras = getIntent().getExtras();
  25.  
  26. if(extras !=null)
  27. {
  28. id = (String) extras.get("user");
  29. uno.setText(id);
  30. }
  31.  
  32.  
  33. final Handler handler = new Handler();
  34. handler.postDelayed(new Runnable() {
  35. public void run() {
  36. new CreateUser().execute();
  37. handler.postDelayed(this, 60000); //now is every 2 minutes
  38. }
  39. }, 60000); //Every 120000 ms (2 minutes)
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. /* Uso de la clase LocationManager para obtener la localizacion del GPS */
  47. LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  48. Localizacion Local = new Localizacion();
  49. Local.setMainActivity(this);
  50. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  51. // TODO: Consider calling
  52. // ActivityCompat#requestPermissions
  53. // here to request the missing permissions, and then overriding
  54. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  55. // int[] grantResults)
  56. // to handle the case where the user grants the permission. See the documentation
  57. // for ActivityCompat#requestPermissions for more details.
  58. return;
  59. }
  60.  
  61. mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
  62. (LocationListener) Local);
  63.  
  64. mensaje1.setText("Localizacion agregada");
  65. mensaje2.setText("");
  66. }
  67.  
  68.  
  69.  
  70. public void setLocation(Location loc) {
  71. //Obtener la direccion de la calle a partir de la latitud y la longitud
  72.  
  73. }
  74.  
  75. /* Aqui empieza la Clase Localizacion */
  76. public class Localizacion implements LocationListener {
  77. MainActivity mainActivity;
  78.  
  79. public MainActivity getMainActivity() {
  80. return mainActivity;
  81. }
  82.  
  83. public void setMainActivity(MainActivity mainActivity) {
  84. this.mainActivity = mainActivity;
  85. }
  86.  
  87. @Override
  88. public void onLocationChanged(Location loc) {
  89. // Este metodo se ejecuta cada vez que el GPS recibe nuevas coordenadas
  90. // debido a la deteccion de un cambio de ubicacion
  91.  
  92.  
  93. loc.getLatitude();
  94. loc.getLongitude();
  95. Double Text = loc.getLatitude();
  96. Double Text1 = loc.getLongitude();
  97. mensaje2.setText(Text1.toString());
  98. mensaje1.setText(Text.toString());
  99. this.mainActivity.setLocation(loc);
  100. long date = System.currentTimeMillis();
  101. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
  102. String dateString = sdf.format(date);
  103. hora.setText(dateString);
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111. @Override
  112. public void onProviderDisabled(String provider) {
  113. // Este metodo se ejecuta cuando el GPS es desactivado
  114. mensaje1.setText("GPS Desactivado");
  115. }
  116.  
  117. @Override
  118. public void onProviderEnabled(String provider) {
  119. // Este metodo se ejecuta cuando el GPS es activado
  120. mensaje1.setText("GPS Activado");
  121. }
  122.  
  123. @Override
  124. public void onStatusChanged(String provider, int status, Bundle extras) {
  125. // Este metodo se ejecuta cada vez que se detecta un cambio en el
  126. // status del proveedor de localizacion (GPS)
  127. // Los diferentes Status son:
  128. // OUT_OF_SERVICE -> Si el proveedor esta fuera de servicio
  129. // TEMPORARILY_UNAVAILABLE -> Temporalmente no disponible pero se
  130. // espera que este disponible en breve
  131. // AVAILABLE -> Disponible
  132. // admin data base result
  133. }
  134.  
  135. }
  136.  
  137.  
  138. class CreateUser extends AsyncTask<String, String, String> {
  139.  
  140. long date = System.currentTimeMillis();
  141. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
  142. String dateString = sdf.format(date);
  143.  
  144.  
  145. @Override
  146. protected void onPreExecute() {
  147. super.onPreExecute();
  148. pDialog = new ProgressDialog(MainActivity.this);
  149. pDialog.setMessage("Subiendo Coordenadas...");
  150. pDialog.setIndeterminate(false);
  151. pDialog.setCancelable(true);
  152. pDialog.show();
  153. }
  154.  
  155. @Override
  156. protected String doInBackground(String... args) {
  157. // TODO Auto-generated method stub
  158. // Check for success tag
  159. int success;
  160. String username = mensaje2.getText().toString();
  161. String password = mensaje1.getText().toString();
  162. String ide = uno.getText().toString();
  163. String hora = dateString;
  164. try {
  165. // Building Parameters
  166. List params = new ArrayList();
  167. params.add(new BasicNameValuePair("username", username));
  168. params.add(new BasicNameValuePair("password", password));
  169. params.add(new BasicNameValuePair("ide", ide));
  170. params.add(new BasicNameValuePair("hora", hora));
  171.  
  172. Log.d("request!", "starting");
  173.  
  174. //Posting user data to script
  175. JSONObject json = jsonParser.makeHttpRequest(
  176. REGISTER_URL, "POST", params);
  177.  
  178. // full json response
  179. Log.d("Registering attempt", json.toString());
  180.  
  181. // json success element
  182. success = json.getInt(TAG_SUCCESS);
  183. if (success == 1) {
  184. Log.d("User Created!", json.toString());
  185. return json.getString(TAG_MESSAGE);
  186. }else{
  187. Log.d("Registering Failure!", json.getString(TAG_MESSAGE));
  188. return json.getString(TAG_MESSAGE);
  189.  
  190. }
  191. } catch (JSONException e) {
  192. e.printStackTrace();
  193. }
  194.  
  195. return null;
  196.  
  197. }
  198.  
  199. protected void onPostExecute(String file_url) {
  200. // dismiss the dialog once product deleted
  201. pDialog.dismiss();
  202. if (file_url != null){
  203. Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
  204. }
  205. }
  206. }
  207.  
  208. }
Add Comment
Please, Sign In to add comment