开发者

Getting location name after every 60000 ms

I am not getting location after every 60000 milliseconds. Can you figure out why?

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

    }
}


It is cause you are doing nothing in the callback. If a new location is found the onLocationChanged is called, in there have to update your data.

@Override
public void onLocationChanged(Location location) {

     this.location = location;
     setCurrentLocation();
}

this should do the trick.

Edit:

after looking more in your code I see something else what has to be changed.

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

should be changed to

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

This is cause your activity implements LocationListener so you just can give this as parameter.


You should be aware that the parameter that you give 60000 is not the exact frequency that you will be notified. This is the minimum time you'll be notified. It can take longer, specially if you are starting to use the GPS (and it is still trying to figure it out where he is).

Also, if GPS is turned off, you won't get these notifications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜