User
- Forum-Beiträge: 4
17.07.2012, 10:56:52 via Website
17.07.2012 10:56:52 via Website
Hallo,
ich bin noch recht neu in der Android-Programmierung und versuche gerade meine erste sinnvolle App zu programmieren.
Was möchte ich tun?
GPS Koordinaten auslesen und jede Minute eine Webseite.
Einen Code der mir die GPS Koordinaten ausliest und anzeigt habe ich gefunden und angepasst.
Aktuell ist dort ein Button "get my location" den klickt man an und die aktuelle Position wird angezeigt.
Anschließend kann ich meine Webseite mit Parametern aufrufen. Das klappt soweit alles.
Kopfzerbrechen bereitet mir das zeitgesteuerte Aufrufen (jede Minute) anstatt den Button zu verwenden.
Mein Code sieht so aus:
Hat jemadn von Euch eine Idee wie ich es am besten bewerkstelligen könnte?
Vielen Dank
ich bin noch recht neu in der Android-Programmierung und versuche gerade meine erste sinnvolle App zu programmieren.
Was möchte ich tun?
GPS Koordinaten auslesen und jede Minute eine Webseite.
Einen Code der mir die GPS Koordinaten ausliest und anzeigt habe ich gefunden und angepasst.
Aktuell ist dort ein Button "get my location" den klickt man an und die aktuelle Position wird angezeigt.
Anschließend kann ich meine Webseite mit Parametern aufrufen. Das klappt soweit alles.
Kopfzerbrechen bereitet mir das zeitgesteuerte Aufrufen (jede Minute) anstatt den Button zu verwenden.
Mein Code sieht so aus:
1// Imports....
2public class HelloAndroidGpsActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
3
4 private EditText editTextShowLocation;
5 private Button buttonGetLocation;
6 private ProgressBar progress;
7
8 private LocationManager locManager;
9 private LocationListener locListener = new MyLocationListener();
10
11 private boolean gps_enabled = false;
12 private boolean network_enabled = false;
13
14 /** Called when the activity is first created. */
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19 editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
20
21 progress = (ProgressBar) findViewById(R.id.progressBar1);
22 progress.setVisibility(View.GONE);
23
24 buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
25 buttonGetLocation.setOnClickListener(this);
26
27 locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
28 }
29
30 public void onClick(View v) {
31 progress.setVisibility(View.VISIBLE);
32 // exceptions will be thrown if provider is not permitted.
33 try {
34 gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
35 } catch (Exception ex) {
36 }
37 try {
38 network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
39 } catch (Exception ex) {
40 }
41
42 // don't start listeners if no provider is enabled
43 if (!gps_enabled && !network_enabled) {
44 AlertDialog.Builder builder = new Builder(this);
45 builder.setTitle("Attention!");
46 builder.setMessage("Sorry, location is not determined. Please enable location providers");
47 builder.setPositiveButton("OK", this);
48 builder.setNeutralButton("Cancel", this);
49 builder.create().show();
50 progress.setVisibility(View.GONE);
51 }
52
53 if (gps_enabled) {
54 locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
55 }
56 if (network_enabled) {
57 locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
58 }
59 }
60
61 class MyLocationListener implements LocationListener {
62
63
64
65 public void onLocationChanged(Location location) {
66 if (location != null) {
67 // This needs to stop getting the location data and save the battery power.
68 locManager.removeUpdates(locListener);
69
70 String londitude = "Londitude: " + location.getLongitude();
71 String latitude = "Latitude: " + location.getLatitude();
72 String altitiude = "Altitiude: " + location.getAltitude();
73 String accuracy = "Accuracy: " + location.getAccuracy();
74 String time = "Time: " + location.getTime();
75
76 editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
77 progress.setVisibility(View.GONE);
78
79 // Webseite aufrufen
80 }
81 }
82
83 public void onProviderDisabled(String provider) {
84 // TODO Auto-generated method stub
85
86 }
87
88 public void onProviderEnabled(String provider) {
89 // TODO Auto-generated method stub
90
91 }
92
93 public void onStatusChanged(String provider, int status, Bundle extras) {
94 // TODO Auto-generated method stub
95
96 }
97 }
98
99 public void onClick(DialogInterface dialog, int which) {
100 if(which == DialogInterface.BUTTON_NEUTRAL){
101 editTextShowLocation.setText("Sorry, location is not determined. To fix this please enable location providers");
102 }else if (which == DialogInterface.BUTTON_POSITIVE) {
103 startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
104 }
105 }
106
107}
2public class HelloAndroidGpsActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
3
4 private EditText editTextShowLocation;
5 private Button buttonGetLocation;
6 private ProgressBar progress;
7
8 private LocationManager locManager;
9 private LocationListener locListener = new MyLocationListener();
10
11 private boolean gps_enabled = false;
12 private boolean network_enabled = false;
13
14 /** Called when the activity is first created. */
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19 editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
20
21 progress = (ProgressBar) findViewById(R.id.progressBar1);
22 progress.setVisibility(View.GONE);
23
24 buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
25 buttonGetLocation.setOnClickListener(this);
26
27 locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
28 }
29
30 public void onClick(View v) {
31 progress.setVisibility(View.VISIBLE);
32 // exceptions will be thrown if provider is not permitted.
33 try {
34 gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
35 } catch (Exception ex) {
36 }
37 try {
38 network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
39 } catch (Exception ex) {
40 }
41
42 // don't start listeners if no provider is enabled
43 if (!gps_enabled && !network_enabled) {
44 AlertDialog.Builder builder = new Builder(this);
45 builder.setTitle("Attention!");
46 builder.setMessage("Sorry, location is not determined. Please enable location providers");
47 builder.setPositiveButton("OK", this);
48 builder.setNeutralButton("Cancel", this);
49 builder.create().show();
50 progress.setVisibility(View.GONE);
51 }
52
53 if (gps_enabled) {
54 locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
55 }
56 if (network_enabled) {
57 locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
58 }
59 }
60
61 class MyLocationListener implements LocationListener {
62
63
64
65 public void onLocationChanged(Location location) {
66 if (location != null) {
67 // This needs to stop getting the location data and save the battery power.
68 locManager.removeUpdates(locListener);
69
70 String londitude = "Londitude: " + location.getLongitude();
71 String latitude = "Latitude: " + location.getLatitude();
72 String altitiude = "Altitiude: " + location.getAltitude();
73 String accuracy = "Accuracy: " + location.getAccuracy();
74 String time = "Time: " + location.getTime();
75
76 editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
77 progress.setVisibility(View.GONE);
78
79 // Webseite aufrufen
80 }
81 }
82
83 public void onProviderDisabled(String provider) {
84 // TODO Auto-generated method stub
85
86 }
87
88 public void onProviderEnabled(String provider) {
89 // TODO Auto-generated method stub
90
91 }
92
93 public void onStatusChanged(String provider, int status, Bundle extras) {
94 // TODO Auto-generated method stub
95
96 }
97 }
98
99 public void onClick(DialogInterface dialog, int which) {
100 if(which == DialogInterface.BUTTON_NEUTRAL){
101 editTextShowLocation.setText("Sorry, location is not determined. To fix this please enable location providers");
102 }else if (which == DialogInterface.BUTTON_POSITIVE) {
103 startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
104 }
105 }
106
107}
Hat jemadn von Euch eine Idee wie ich es am besten bewerkstelligen könnte?
Vielen Dank
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.