2011-07-07 12 views
0

60000ミリ秒ごとに位置が取得されません。なぜあなたは理解できますか?60000ミリ秒ごとに位置名を取得

package com.module.rapidera.RapidTrack; 

import java.util.List; 
import java.util.Locale; 

import com.module.rapidera.RapidTrack.R.string; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

public class RapidTrackActivity extends Activity implements LocationListener{ 
    /** Called when the activity is first created. */ 

    private LocationManager locationManager; 
    public String latitude; 
    public String longitude; 
    private String provider; 
    public Location location; 
    public String city; 

    private TextView latituteField; 
    private TextView longitudeField; 
    private TextView myAddress; 
    final int maxResult =1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     location = locationManager.getLastKnownLocation(provider); 
     LocationListener mlocListener = new RapidTrackActivity(); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 0, mlocListener); 

     setCurrentLocation(); 
    } 

    public void setCurrentLocation() 
     { 
      Double latDouble = location.getLatitude(); 
      Double lngDouble = location.getLongitude(); 
      String latString = latDouble.toString(); 
      String lngString = lngDouble.toString(); 
      try 
      { 
       Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); 
       List<Address> addresses = geoCoder.getFromLocation(latDouble, lngDouble, 1); 

       Address addr = addresses.get(0); 

       String country = addr.getCountryName(); 
       String city = addr.getLocality(); 
       String local= addr.getSubLocality(); 
       myAddress.setText(""+city +"" + country); 
       String s=local.concat(",").concat(city).concat(",").concat(country); 
       Log.v("country name is......", "Country -- " + country + " City -- " + city); 
       Toast.makeText(this, "," + local + "," + city + "," + country, Toast.LENGTH_LONG).show(); 
       sendSMS("919762203359",s); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       Toast.makeText(this, e.toString(),Toast.LENGTH_LONG); 
      } 
     } 

    private void sendSMS(String phoneNumber, String message) 
     {  
      /* 
      PendingIntent pi = PendingIntent.getActivity(this, 0, 
        new Intent(this, test.class), 0);     
       SmsManager sms = SmsManager.getDefault(); 
       sms.sendTextMessage(phoneNumber, null, message, pi, null);   
      */ 
      try 
      { 
      String SENT = "SMS_SENT"; 
      String DELIVERED = "SMS_DELIVERED"; 

      PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
       new Intent(SENT), 0); 

      PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
       new Intent(DELIVERED), 0); 

      //---when the SMS has been sent--- 
      registerReceiver(new BroadcastReceiver(){ 
       @Override 
       public void onReceive(Context arg0, Intent arg1) { 
        switch (getResultCode()) 
        { 
         case Activity.RESULT_OK: 
          Toast.makeText(getBaseContext(), "SMS sent", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
          Toast.makeText(getBaseContext(), "Generic failure", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_NO_SERVICE: 
          Toast.makeText(getBaseContext(), "No service", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_NULL_PDU: 
          Toast.makeText(getBaseContext(), "Null PDU", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_RADIO_OFF: 
          Toast.makeText(getBaseContext(), "Radio off", 
            Toast.LENGTH_SHORT).show(); 
          break; 
        } 
       } 
      }, new IntentFilter(SENT)); 

      //---when the SMS has been delivered--- 
      registerReceiver(new BroadcastReceiver(){ 
       @Override 
       public void onReceive(Context arg0, Intent arg1) { 
        switch (getResultCode()) 
        { 
         case Activity.RESULT_OK: 
          Toast.makeText(getBaseContext(), "SMS delivered", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case Activity.RESULT_CANCELED: 
          Toast.makeText(getBaseContext(), "SMS not delivered", 
            Toast.LENGTH_SHORT).show(); 
          break;      
        } 
       } 
      }, new IntentFilter(DELIVERED));   

      SmsManager sms = SmsManager.getDefault(); 
      sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);    
     }  

    catch (Exception e) { 
     Toast.makeText(getBaseContext(),e.toString() , Toast.LENGTH_LONG).show(); 
    } 
     } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 
} 

答えて

1

コールバックに何もしていないことが原因です。新しい場所が見つかった場合は、onLocationChangedが呼び出されます。データを更新する必要があります。

@Override 
public void onLocationChanged(Location location) { 

    this.location = location; 
    setCurrentLocation(); 
} 

これはトリックを行う必要があります。

編集:

あなたのコード内でより多くのを見た後、私は変更する必要があるものを何か他のものを参照してください。

 LocationListener mlocListener = new RapidTrackActivity(); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 0, mlocListener); 

これはあなただけのパラメータとしてこれを与えることができますので、あなたの活動はLocationListenerを実装原因である

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 0, this); 

に変更する必要があります。

0

60000のパラメータは、通知される正確な頻度ではありません。これが通知される最小時間です。特にGPSを使用し始めている場合は、時間がかかることがあります(まだどこにいるか把握しようとしています)。

また、GPSがオフになっていると、これらの通知を受け取ることはできません。

関連する問題