【问题标题】:Finding current location from background service android从后台服务android查找当前位置
【发布时间】:2012-10-30 05:21:13
【问题描述】:

我想通过后台服务活动找到当前位置并与我的数据库中的一些数据进行比较...但不知道如何做...所以请给我一些建议...这是我现在过得怎么样

    package com.example.alert;

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.widget.Toast;

    public class service extends Service{
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onCreate() {
    Toast.makeText(this, "Background Service Created", Toast.LENGTH_LONG).show();



}

public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();


}

public void onStart(Intent intent, int startid) {

    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    LocationListener ll = new MyLocationListener();
    //lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5*60*10000, 0, ll);
    lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, ll);



}


public class MyLocationListener implements LocationListener
{

    public void onLocationChanged(Location loc)
    {
        if(loc!=null)
        {
            double x;


            String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
            Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
        /*  cr.moveToFirst();
            while(!cr.isAfterLast()) {
                 al.add(cr.getString(cr.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
                 cr.moveToNext();
            }*/
        //  lm.addProximityAlert(loc.getLatitude(), loc.getLongitude(), 10009,, intent)

            //for loop, alerters retrieving one by one
//      x= calcDistance(loc.getLatitude(),loc.getLongitude(),z,y);
    //      if(x<1){
        //        Intent myIntent = new Intent(ViewAlerters.this,CallMode.class);
          //        startActivity(myIntent);
            }

        }



    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

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

    }

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

    }


    }
    }

【问题讨论】:

  • 查看 Sam 对我提出的问题的回答 here。它应该可以帮助你

标签: android service geolocation location background-process


【解决方案1】:

Reto Meier(Android 开发者关系)在这方面做了很多工作,我建议你去看看。

在这里写博客http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

示例代码在这里https://code.google.com/p/android-protips-location/

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多