English 中文(简体)
Fetch 甲状腺和停止更新
原标题:Fetch first location on android and stop updates
  • 时间:2011-11-07 12:54:07
  •  标签:
  • android

我仍对甲状腺的首选位置存在问题。 我正在使用类似标准。

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setSpeedRequired(true);
        String provider = lm.getBestProvider(criteria, true);

        if(provider!=null && provider.length()>0){
            lm.requestLocationUpdates(provider, 0, 0, this);
        }

I need to stop when first time get location like lm.removeUpdates(this); Where to put that line of code ? In onLocationChanged ?

最佳回答
public class GPSLocatorActivity extends Activity {
    double current_lat, current_lng;
    MapView mapView;
    boolean flag = true;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Use the LocationManager class to obtain GPS locations */

        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        LocationListener mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,
                mlocListener);

    }

    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener

    {


        public void onLocationChanged(Location loc) {
            current_lat = loc.getLatitude();
            current_lng = loc.getLongitude();

            if (flag == true) {
                flag = false;
                go(current_lat, current_lng);

            }
        }


        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled !!! Please Enable It.",
                    Toast.LENGTH_SHORT).show();

        }


        public void onProviderEnabled(String provider)

        {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();

        }


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

        }

    }/* End of Class MyLocationListener */

    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;

    public void go(double c_lat, double c_lng) {
              System.out.println("your current Lat :: " + c_lat);    
              System.out.println("your current Lng :: " + c_lng);

             /*turn of gps now */ 
               turnGPSOff();
         }
    private void turnGPSOff() {
        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if (provider.contains("gps")) { // if gps is enabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }
    }

}/* End of UseGps Activity */
问题回答

是的,将在每个地点更新<代码>。 如果你只想,你就可以去除。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签