· 7 years ago · Feb 01, 2019, 07:04 PM
1// GPS
2 private void addGPSListener() {
3 globalconstant.db.setVersion(1);
4 globalconstant.db.setLocale(Locale.getDefault());
5 globalconstant.db.setLockingEnabled(true);
6 final String gps =
7
8 "CREATE TABLE IF NOT EXISTS GPS_Values ("
9
10 + "id INTEGER PRIMARY KEY AUTOINCREMENT, Latitude float(10, 8), Longitude float(10, 8),Accuracy INTEGER,Speed INTEGER,City TEXT,timestamp TIMESTAMP);";
11 globalconstant.db.execSQL(gps);
12
13
14 float f = Float.valueOf(globalconstant.gps_update_value.trim())
15 .floatValue();
16 Log.d("FESTIVALE :: ", "FrissÃtési idÅ‘: "
17 + f);
18 float update = f;
19
20
21 globalconstant.mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
22 globalconstant.mlocListener = new MyLocationListener();
23 globalconstant.mlocManager.requestLocationUpdates(
24 LocationManager.GPS_PROVIDER, (long) update, 0,
25 globalconstant.mlocListener);
26}
27
28public class MyLocationListener implements LocationListener {
29
30 public void onLocationChanged(Location loc) {
31
32 float szel = (float) loc.getLatitude();
33 float hossz = (float) loc.getLongitude();
34 int horiAcc = (int) (loc.getAccuracy());
35 // int speed=(int) ((loc.getSpeed()*3600)/1000); //sebesség km/h-ban
36 int speed = 0;
37
38 if (loc.hasSpeed()) {
39 speed = (int) ((loc.getSpeed() * 3600) / 1000); // sebesség
40 // km/h-ban
41 } else {
42 speed = 0;
43 }
44
45 String test = String.format("%.08f", szel);
46 String test2 = String.format("%.08f", hossz);
47
48 Geocoder geocoder = new Geocoder(main.this, Locale.getDefault());
49 try {
50 List<Address> addresses = geocoder.getFromLocation(szel, hossz,
51 1);
52 city = addresses.get(0).getLocality();
53 } catch (IOException e) {
54 e.printStackTrace();
55 }
56
57 ContentValues gps_values = new ContentValues();
58
59 gps_values.put("Latitude", test);
60 gps_values.put("Longitude", test2);
61 gps_values.put("Accuracy", horiAcc);
62 gps_values.put("Speed", speed);
63 gps_values.put("City", city);
64
65 SimpleDateFormat dateFormat = new SimpleDateFormat(
66 "yyyy-MM-dd HH:mm:ss");
67 Date date = new Date(System.currentTimeMillis());
68
69 gps_values.put("timestamp", dateFormat.format(date));
70
71 try {
72 globalconstant.db.beginTransaction();
73 globalconstant.db.insert("GPS_Values", null, gps_values);
74 globalconstant.db.setTransactionSuccessful();
75 } finally {
76 globalconstant.db.endTransaction();
77 }
78
79 Log.d("FESTIVALE :: ", "Hely " + test + ", " + test2 + " , "
80 + horiAcc + " , " + speed + " , " + city + "," + dateFormat.format(date));
81 // String Text = "My current location is: " + "Latitude = "
82 // + loc.getLatitude() + "nLongitude = " + loc.getLongitude();
83
84 // Toast.makeText(getApplicationContext(), "Hely" +test + "n" +
85 // test2 + "n" + horiAcc + "n" +speed + "n" +city,
86 // Toast.LENGTH_SHORT)
87 // .show();
88
89 }
90
91 public void onProviderDisabled(String provider) {
92 Toast.makeText(getApplicationContext(), "Gps Disabled",
93 Toast.LENGTH_SHORT).show();
94 DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
95 public void onClick(DialogInterface dialog, int which) {
96 switch (which) {
97 case DialogInterface.BUTTON_POSITIVE:
98 // show gps otions
99 Intent gpsOptionsIntent = new Intent(
100 android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
101 startActivity(gpsOptionsIntent);
102 break;
103
104 case DialogInterface.BUTTON_NEGATIVE:
105 dialog.cancel();
106 break;
107 }
108 }
109 };
110
111 AlertDialog.Builder builder = new AlertDialog.Builder(main.this);
112 builder.setMessage("A GPS nincs aktiválva!nAktiválja most?")
113 .setPositiveButton("Aktivál", dialogClickListener)
114 .setNegativeButton("Nem", dialogClickListener).show();
115 }
116
117 public void onProviderEnabled(String provider) {
118 Toast.makeText(getApplicationContext(), "Gps Enabled",
119 Toast.LENGTH_SHORT).show();
120
121 }
122
123 public void onStatusChanged(String provider, int status, Bundle extras) {
124 }
125
126}// gps vége
127
128globalconstant.mlocManager.requestLocationUpdates(
129 LocationManager.GPS_PROVIDER, (long) update, 0,
130 globalconstant.mlocListener);
131
132requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)