每隔60000 ms后获取位置名称

时间:2011-07-07 10:24:18

标签: android

我每隔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

    }
}

2 个答案:

答案 0 :(得分:1)

这是因为你在回调中什么都不做。如果找到新位置,则调用onLocationChanged,必须更新数据。

@Override
public void onLocationChanged(Location location) {

     this.location = location;
     setCurrentLocation();
}

这应该可以解决问题。

编辑:

在查看代码后,我会看到其他一些必须改变的内容。

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

应改为

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

这是因为您的活动实现了LocationListener,因此您可以将其作为参数。

答案 1 :(得分:0)

您应该知道,您提供的参数60000并不是您通知的确切频率。这是您收到通知的最短时间。它可能需要更长的时间,特别是如果你开始使用GPS(它仍然试图找出它在哪里)。

此外,如果GPS已关闭,您将无法收到这些通知。