为什么后台运行服务无法获取和/或更新位置?

时间:2014-03-22 01:01:38

标签: android service locationmanager forceclose

我是Android的新手。我在后台运行服务以获得不断变化的纬度和经度。我之前已经问过这个问题,但无法得到适当的答案。我在后台有一项服务,应该跟踪不断变化的位置。我意识到坐标没有改变或者它们被设置为null。我尝试了不同的建议,但同样的问题仍然存在。请有人可以一步一步解释我该怎么做。过去一周我一直坐在这里。我的错误日志和代码如下:

错误日志

 03-22 06:26:29.622: E/AndroidRuntime(12483): FATAL EXCEPTION: main
 03-22 06:26:29.622: E/AndroidRuntime(12483): java.lang.NullPointerException
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at com.example.broadcast.Ser.onLocationChanged(Ser.java:131)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:263)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:196)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:212)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at android.os.Handler.dispatchMessage(Handler.java:99)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at android.os.Looper.loop(Looper.java:137)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at android.app.ActivityThread.main(ActivityThread.java:4960)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at java.lang.reflect.Method.invokeNative(Native Method)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at java.lang.reflect.Method.invoke(Method.java:511)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 03-22 06:26:29.622: E/AndroidRuntime(12483):   at dalvik.system.NativeStart.main(Native Method)

我的服务代码:

 public class Ser extends Service implements LocationListener {

 public Location getLocation(String provider) {
    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider,
                MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(provider);


            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            String l1=String.valueOf(latitude);
            String l2=String.valueOf(longitude);

        }

        }


    return location;
}

  @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub


    DatabaseHandler db=new DatabaseHandler(getApplicationContext());
    locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    getLocation(LocationManager.NETWORK_PROVIDER);
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    isNetworkEnabled = locationManager
    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if(isNetworkEnabled==true && isGPSEnabled==false)
    {
        getLocation(LocationManager.NETWORK_PROVIDER);
    }
    else if(isGPSEnabled==true && isNetworkEnabled==false)
    {
        getLocation(LocationManager.GPS_PROVIDER);
    }
    else if(isGPSEnabled==true && isNetworkEnabled==false)
    {
        getLocation(LocationManager.GPS_PROVIDER);
    }
    else if(isGPSEnabled==true && isNetworkEnabled==true )
    {
        getLocation(LocationManager.GPS_PROVIDER);
    }
    else
    {
        cellid();
    }



    return super.onStartCommand(intent, flags, startId);
}
 @Override
public void onLocationChanged(Location location) {
    //this.location=location;
    if(location!=null)
    {
    double latitude =location.getLatitude();
    double longitude = location.getLongitude();
    DatabaseHandler db=new DatabaseHandler(getApplicationContext());
    String l1=String.valueOf(latitude);
    String l2=String.valueOf(longitude);
    val=db.taskid(l1, l2);
    String silent="Silent",vibrate="Vibrate";

    if(val.get("heading").compareTo(silent)==0)

{
        AudioManager audioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
    }

    else if(val.get("heading").compareTo(vibrate)==0)
    {
        AudioManager audioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); 
    }
    }
}

1 个答案:

答案 0 :(得分:0)

我有一个类似的应用,它使用服务来检索位置。在oncreate方法中,初始化谷歌服务和位置管理器,如下所示:

private LocationManager locationManager;
private String provider;
private Location location;
private int TIME_IN_MILLI = 20000;

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

    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Google Play Services are unavailable
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
        int requestCode = 10;
        Log.i("Info", "Google Play Services are unavailable");
    }else { 
        //Get GPS Location manager
        locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider, TIME_IN_MILLI, 0, this);
    }
}

初始化后,当您获取位置并在TIME_IN_MILLI变量中设置20秒后,将会点击您的onLocationChanged回调。

相关问题