2011-10-12 14 views
8

मैं एक ऐप लिख रहा हूं जो डॉट पर हर दस मिनट में एक नया जीपीएस स्थान रिकॉर्ड करता है।एंड्रॉइड में समय-समय पर जीपीएस स्थान कैसे ढूंढें

मैंने स्थान परागकों की कोशिश की है, एक सेवा में स्थान श्रोता डाल दिया है, और एक स्थान में स्थान श्रोता डाल दिया है लेकिन इनमें से कोई भी तरीका मेरे लिए काम नहीं करता है।

मुझे अपने स्थान डेटा को रिकॉर्ड करना शुरू करने से 2 मिनट पहले जीपीएस स्थान श्रोता को चालू करने के लिए एक विधि की आवश्यकता है (जीपीएस को स्थान खोजने के लिए पर्याप्त समय दें या कोई सिग्नल निर्धारित न करें)। मैं एक अलार्म का उपयोग करके ऐसा कर रहा हूं जो मेरी जीपीएस एक्टिविटी को कॉल करता है इससे पहले कि मैं अपना अपडेटटेलोकेशन क्लास कॉल करूं। मेरी updatelocation कक्षा तब gpsActivity के locationmanager लेता है और इससे एक स्थान निकालता है।

सिद्धांत रूप में यह काम करना चाहिए, लेकिन जब मैं अपने फोन पर डाल दिया मैं हमेशा बुरा डेटा (सटीकता बहुत कम है या कोई संकेत बिल्कुल)

किसी ने मुझे बाहर करने में मदद कर सकता है मैं बहुत आभारी होंगे मिलता है।

धन्यवाद

अलार्म अद्यतन स्थान से पहले इस वर्ग के 2 मिनट कॉल

public class GPSActivity extends Activity { 

    public static LocationListener loc_listener = null; 
    public static LocationManager locationManager = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     getGPS(); 
    } 

    public static void getGPS() { 
     if (loc_listener == null) { 
      loc_listener = new LocationListener() { 

       @Override 
       public void onStatusChanged(String provider, int status, 
         Bundle extras) {} 

       @Override 
       public void onProviderEnabled(String provider) {} 

       @Override 
       public void onProviderDisabled(String provider) {} 

       @Override 
       public void onLocationChanged(Location location) {} 
      }; 
     } 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 0, 0, loc_listener); 
    } 

    public static void killGPS() { 
     if (locationManager != null && loc_listener != null) { 
      locationManager.removeUpdates(loc_listener); 
     } 
    } 
} 

फिर दो मिनट बाद में इस सेवा

public class UpdateLocation extends IntentService { 

    public static final String id = ""; 
    public static int retryCount = 0; 
    int notificationID = 1; 
    // gets the location manager from the GPSActivity 
    LocationManager locationManager = GPSActivity.locationManager; 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    public UpdateLocation() { 
     super("UpdateLocation"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     locationManager = GPSActivity.locationManager; 
     SharedPreferences prefs = getSharedPreferences("Settings", 0); 
     final String id = prefs.getString("ID", ""); 
     HttpParams httpParams = new BasicHttpParams(); 
     // 30seconds and it stops 
     HttpConnectionParams.setConnectionTimeout(httpParams, 30000); 
     HttpConnectionParams.setSoTimeout(httpParams, 30000); 
     DefaultHttpClient httpclient = new DefaultHttpClient(httpParams); 
     HttpPost httpost = new HttpPost(
       "http://iphone-radar.com/gps/gps_locations"); 
     JSONObject holder = new JSONObject(); 
     try { 
      holder.put("id", id); 
      Location location = getLocation(); 
      if (location != null && (location.getAccuracy() < 25)) { 
       retryCount = 0; 
       Calendar c = Calendar.getInstance(); 
       SimpleDateFormat sdf = new SimpleDateFormat(
         "hh:mmaa MM/dd/yyyy"); 
       holder.put("time", sdf.format(c.getTime())); 
       holder.put("time_since_epoch", 
         System.currentTimeMillis()/1000); 
       holder.put("lat", location.getLatitude()); 
       holder.put("lon", location.getLongitude()); 
       StringEntity se = new StringEntity(holder.toString()); 
       httpost.setEntity(se); 
       httpost.setHeader("Accept", "application/json"); 
       httpost.setHeader("Content-type", "application/json"); 
       ResponseHandler responseHandler = new BasicResponseHandler(); 
       String response = httpclient.execute(httpost, 
         responseHandler); 
       org.json.JSONObject obj; 
       obj = new org.json.JSONObject(response); 
       SimpleDateFormat sdf2 = new SimpleDateFormat(
         "yyyy-MM-dd hh:mm:ssaa"); 
       addHistory(
         sdf2.format(c.getTime()), 
         "Background GPS", 
         "Latitude: " 
          + String.format("%.6g%n", 
            location.getLatitude()) 
          + "\n" 
          + "Longitude: " 
          + String.format("%.6g%n", 
            location.getLongitude())); 
       SharedPreferences.Editor editor = prefs.edit(); 
       editor.putString("LastUpdatedTime", sdf.format(c.getTime())); 
       editor.commit(); 
       Intent setAlarm = new Intent(UpdateLocation.this, 
         UpdateLocation.class); 
       PendingIntent pendingIntent = PendingIntent.getService(
         UpdateLocation.this, 0, setAlarm, 0); 
       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
       Calendar calendar = Calendar.getInstance(); 
       calendar.setTimeInMillis(System.currentTimeMillis()); 
       int UPDATE_TIME = prefs.getInt("Update_time", 10); 
       calendar.add(Calendar.MINUTE, UPDATE_TIME); 
       alarmManager.set(AlarmManager.RTC_WAKEUP, 
         calendar.getTimeInMillis(), pendingIntent); 
       // sets an alarm for the next time we need to record a 
       // location 
       GPSActivity.killGPS(); 
       // turns off the gps location listener 
       Intent setAlarm2 = new Intent(UpdateLocation.this, 
         GPSActivity.class); 
       PendingIntent pendingIntent2 = PendingIntent.getService(
         UpdateLocation.this, 0, setAlarm2, 0); 
       Calendar calendar2 = Calendar.getInstance(); 
       calendar2.add(Calendar.MINUTE, UPDATE_TIME - 2); 
       alarmManager.set(AlarmManager.RTC_WAKEUP, 
         calendar2.getTimeInMillis(), pendingIntent2); 
       // sets an alarm to turn on the gpsActivity 2mins before the 
       // next time updatelocation needs to record some data 
      } 
     } finally { 
     } 
    } 
} 
+0

http://tinyurl.com/942lhyf इस कामकाजी डेमो को आजमाएं :) –

उत्तर

4

उपयोग रिसीवर और सेवा एक साथ कहा जाता है। इस उद्देश्य के लिए आप this link में एक पूर्ण नमूना पा सकते हैं। इसमें एक श्रोता है। श्रोता को आपकी गतिविधि में देखा जा सकता है ताकि यह देखा जा सके कि आपके लिए एक नया स्थान तैयार है।

+0

http://stackoverflow.com/questions/2775628/android-how-to-periodically-send-location-to-a- सर्वर ऐसा लगता है सहमत नहीं हैं। यह अलार्म प्रबंधक की सिफारिश करता है – Mawg

संबंधित मुद्दे