Advertisement
Guest User

Untitled

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