warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 και με gps tracker main package example.test; import android.app.Activity; import android.location.Location; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.getlocation: GPSTracker gps = new GPSTracker(this); Location myLocation = gps.getLocation(); break; default: return super.onOptionsItemSelected(item); } return true; } } gps tracker, το άλλαξα κι αυτό λίγο package example.test; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; import android.util.Log; import android.widget.Toast; public class GPSTracker extends Service implements LocationListener { private final Context mContext; private ProgressDialog progressDialog; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location; // location double latitude; // latitude double longitude; // longitude // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; public GPSTracker(Context context) { this.mContext = context; //getLocation(); den ti xreiazesai } public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { progressDialog = new ProgressDialog(mContext); progressDialog.setMessage("Waiting for location..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.show(); this.canGetLocation = true; // First get location from Network Provider if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener * Calling this function will stop using GPS in your app * */ public void stopUsingGPS(){ if(locationManager != null){ locationManager.removeUpdates(GPSTracker.this); } } /** * Function to get latitude * */ public double getLatitude(){ if(location != null){ latitude = location.getLatitude(); } // return latitude return latitude; } /** * Function to get longitude * */ public double getLongitude(){ if(location != null){ longitude = location.getLongitude(); } // return longitude return longitude; } /** * Function to check GPS/wifi enabled * @return boolean * */ public boolean canGetLocation() { return this.canGetLocation; } /** * Function to show settings alert dialog * On pressing Settings button will lauch Settings Options * */ public void showSettingsAlert(){ AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Title alertDialog.setTitle("GPS is settings"); // Setting Dialog Message alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing Settings button alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } @Override public void onLocationChanged(Location location) { stopUsingGPS(); progressDialog.cancel; Toast.makeText(mContext, "Location: " + latitude + " " + longitude , Toast.LENGTH_LONG).show(); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public IBinder onBind(Intent arg0) { return null; } } έβγαλα το get location από τον constructor έβαλα το progress dialog μέσα στην get location στην location changed σταματάει τα update, κλείνει το dialog, πετάει το toast στη main έχεις και το location να το κάνεις ο,τι θες επίσης ξέχασα να βάλω cancel listener στο dialog που να κάνει stop το gps
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 Καταρχήν ευχαριστώ πολύ! Αλλά το θέμα είναι ότι πατώντας το κουμπί για να ξεκινήσει να βρει την τοποθεσία "πρέπει " να ξεκινήσει το asynctask από ότι έχω καταλάβει. Δηλ έτσι όπως καλείς τώρα case R.id.getlocation: GPSTracker gps = new GPSTracker(this); Location myLocation = gps.getLocation(); απλά δεν τρέχει. Αν το κάνω έτσι (όπως περίπου το είχα πριν , αν θες κοίταξε πως το έχω): gps = new GPSTracker(ShowList.this); // check if GPS enabled if(gps.canGetLocation()){ Location myLocation=gps.getLocation(); }else{ gps.showSettingsAlert(); } Ενεργοποιείς το GPS από τις ρυθμίσεις αλλά πατώντας το κουμπί σε ξαναβγάζει το διάλογο για να ενεργοποιήσεις το GPS.Δεν αρχίζει να ψάχνει. Το θέμα είναι μέσα στην asynctask που έχω πως μπορεί να υλοποιηθεί όλο αυτό.
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 χωρίς asynctask είναι το παραπάνω όπως είναι και τα δύο, δεν χρειάζεται τίποτα άλλο
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 Kι όμως , δε λειτουργεί . Δεν κάνει απολύτως τίποτα πατώντας το κουμπί. Δεν ξέρω αν παίζει ρόλο αλλά το έχω σε onClick και όχι σε public boolean onOptionsItemSelected(MenuItem item) public void onClick(View v) { switch (v.getId()){case R.id.getlocation: gps = new GPSTracker(this); ...
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 θες να το ποστάρεις όλο όπως είναι? την onclick τη δήλωσες στο xml? το gpstracker το άλλαξες? εδώ στον emulator παίζει μια χαρά
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 H onClick δουλεύει μια χαρά , όλα λειτουργούν οκ. Το άλλαξα το GPSTracker όπως είπες. (Δεν ξέρω τι θέλεις να ποστάρω) Κοίτα,αν χρησιμοποιήσω αυτό που είπες: case R.id.getlocation: gps = new GPSTracker(this); Location myLocation=gps.getLocation(); απλά δε γίνεται τίποτα,κανένα συμβάν. Αυτό που έχω εγώ (με το προηγούμενο GPSTracker) : case R.id.getlocation: gps = new GPSTracker(this); // check if GPS enabled if(gps.canGetLocation()){ GetGPSData gps2 = new GetGPSData(); gps2.execute(); }else{ gps.showSettingsAlert(); } λειτουργεί μια χαρά.Απλά σου υπενθυμίζω ότι η ProgressDialog δε σταματά ποτέ να γυρνάει και πρέπει να πατήσω back button για να σταματήσει και μετά ξανά το κουμπί (getlocation) για μου δείξει το toast με τις συντεταγμένες. public class GetGPSData extends AsyncTask<Void, Integer, Void> { ProgressDialog progressDialog = null; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(ShowList.this); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { GetGPSData.this.cancel(true); } }); progressDialog.setMessage("Waiting for location..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.show(); } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); } @Override protected Void doInBackground(Void ... params) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); while (latitude == 0 || longitude == 0) { try { Thread.sleep(1000); }catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } protected void onCancelled() { Toast.makeText(getBaseContext(), "Cancelled/Error connecting", Toast.LENGTH_LONG).show(); progressDialog.dismiss(); } @Override protected void onPostExecute(Void result) { progressDialog.dismiss(); Toast.makeText(ShowList.this, "Your Location is \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } }
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 δεν χρειάζεσαι καθόλου το asynctask αυτά που σου έστειλα μόνο βάζεις το πρώτο στο mainactivity και το δεύτερο στο gpstracker.java
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 Όπως σου είπα το δοκίμασα αλλά δεν συμβαίνει τίποτα πατώντας το κουμπί. Ούτε έλεγχος αν είναι ανοιχτό το GPS ,ούτε να αρχίσει να ψάχνει σήμα, τιποτα..
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 θες να ποστάρεις όλο το main activity και όλο το gps tracker? στο κινητό το δοκιμάζεις ή στο emulator? δεν κάνει έλεγχο όπως είναι τώρα, θέλει μια if πριν το getlocation στην onclick
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 Το GPSTracker το δικό σου ή το `παλιό`? Στο κινητό το δοκιμάζω.
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 αν είναι όπως στο έστειλα μην το ποστάρεις, αλλά να είναι σίγουρα ίδιο τη main θέλω πιο πολύ
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 OK, (ειναι σε άλλη activity ,όχι στη main): public class ShowList extends Activity implements OnClickListener{ .... GPSTracker gps; SqlHandler sqlHandler; EditText title,location,comments; Button OKbtn,getlocation,photobtn; double latitude = 0.0; double longitude = 0.0; ArrayList<myItems> myList = new ArrayList<myItems>(); ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.showlist); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_showlist); title = (EditText) findViewById(R.id.title); comments = (EditText) findViewById(R.id.comments); View inflatedView = getLayoutInflater().inflate(R.layout.showadapterlist, null); this.imageView = (ImageView) inflatedView.findViewById(R.id.myimage); OKbtn = (Button) findViewById(R.id.OKbtn); ... getlocation = (Button) findViewById(R.id.getlocation); getlocation.setOnClickListener(this); sqlHandler = new SqlHandler(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.OKbtn: ... break; case R.id.photobtn: ... break; case R.id.getlocation: // create class object gps = new GPSTracker(this); //Location myLocation=gps.getLocation(); // check if GPS enabled if(gps.canGetLocation()){ GetGPSData gps2 = new GetGPSData(); gps2.execute(); //gps2.cancel(true); }else{ gps.showSettingsAlert(); } break; } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == CAMERA_REQUEST){ if (resultCode==RESULT_OK){ .... } } if (resultCode == RESULT_CANCELED) { } } public class GetGPSData extends AsyncTask<Void, Integer, Void> { ProgressDialog progressDialog = null; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(ShowList.this); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { GetGPSData.this.cancel(true); } }); progressDialog.setMessage("Waiting for location..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.show(); } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); } @Override protected Void doInBackground(Void ... params) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); while (latitude == 0 || longitude == 0) { try { Thread.sleep(1000); }catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } protected void onCancelled() { Toast.makeText(getBaseContext(), "Cancelled/Error connecting", Toast.LENGTH_LONG).show(); progressDialog.dismiss(); } @Override protected void onPostExecute(Void result) { progressDialog.dismiss(); Toast.makeText(ShowList.this, "Your Location is \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } } } Λοιπόν , έκανα ακριβώς copy paste το GPSTracker όπως το έχεις και τώρα: Μπαίνοντας στην ShowList activity έχει ξεκινήσει ήδη η dialog να γυρνάει (χωρίς να έχω πατησει τιποτα) Πατώντας το κουμπί για να ξεκινήσει να ψάχνει , κάνει τον έλεγχο , ενεργοποιώ το GPS αλλά μόλις το ξαναπατήσω για να ψάξει ξανακάνει τον έλεγχο και με πετάει στις ρυθμίσεις.
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 συνεχίζεις να έχεις την asynctask επίσης τωρα είδα οτι δεν δουλεύει η cangetlocation στο gpstracker που πείραξα γιατί έχει βάλει τον έλεγχο μέσα στην getlocation και γι αυτό την καλούσε στον constructor πρέπει να γίνει σε άλλη συνάρτηση για να το ελέγχει το τελευταίο gpstracker που παίζουν όλα package example.test; import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; import android.util.Log; import android.widget.Toast; public class GPSTracker extends Service implements LocationListener { private final Context mContext; private ProgressDialog progressDialog; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location; // location double latitude; // latitude double longitude; // longitude // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; public GPSTracker(Context context) { this.mContext = context; this.canGetLocation = canGetLocation(); } public Location getLocation() { try { if (!canGetLocation) { // no network provider is enabled } else { progressDialog = new ProgressDialog(mContext); progressDialog.setMessage("Waiting for location..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.show(); if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener * Calling this function will stop using GPS in your app * */ public void stopUsingGPS(){ if(locationManager != null){ locationManager.removeUpdates(GPSTracker.this); } } /** * Function to get latitude * */ public double getLatitude(){ if(location != null){ latitude = location.getLatitude(); } // return latitude return latitude; } /** * Function to get longitude * */ public double getLongitude(){ if(location != null){ longitude = location.getLongitude(); } // return longitude return longitude; } /** * Function to check GPS/wifi enabled * @return boolean * */ public boolean canGetLocation() { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) return false; else return true; } /** * Function to show settings alert dialog * On pressing Settings button will lauch Settings Options * */ public void showSettingsAlert(){ AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Title alertDialog.setTitle("GPS is settings"); // Setting Dialog Message alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing Settings button alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } @Override public void onLocationChanged(Location location) { stopUsingGPS(); progressDialog.cancel(); Toast.makeText(mContext, "Location: " + latitude + " " + longitude , Toast.LENGTH_LONG).show(); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public IBinder onBind(Intent arg0) { return null; } } σου στέλνω και το activity σε λίγο σβήσε όλο το async task, βάλε το gpstracker το τελευταίο που σου έστειλα και άλλαξε στο activity να το κάνεις έτσι case R.id.getlocation: // create class object GPSTracker gps = new GPSTracker(this); if (gps.canGetLocation) { gps.getLocation(); } else { gps.showSettingsAlert(); } break;
ggeo1 Δημοσ. 20 Μαΐου 2013 Μέλος Δημοσ. 20 Μαΐου 2013 Tην έχω κάνει ´σχόλια´ την asynctask, δεν εκτελείται και πάλι τα ίδια αποτελέσματα έχω. πρέπει να γίνει σε άλλη συνάρτηση για να το ελέγχει Πως ακριβώς δηλ? Επίσης, μου ξεκινάει αμέσως το dialogprorress ,ενώ πρέπει να το ξεκινήσει αφού αρχίσει να ψάχνει. Και τέλος , μου εμφανίζει ένα περίεργο dialogprorress ! Όχι το παραθυράκι που έχει τον τίτλο( Waiting for location...") και γυρνάει αριστερά η `ροδα`.
warlock9_0 Δημοσ. 20 Μαΐου 2013 Δημοσ. 20 Μαΐου 2013 δεν ξερω τι άλλο να πω, εδώ παίζει ακριβώς όπως θες βάλε το τελευταίο gpstracker που σου έστειλα και άλλαξε και τη γραμμη με το toast που μου ξέφυγε, κάντην Toast.makeText(mContext, "Location: " + location.getLatitude() + " " + location.getLongitude() , Toast.LENGTH_LONG).show();
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα