当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Why does background running service cannot get and/or update the locations?

问题描述:

I am a newbie to Android. I am running a service in the background to get the changing latitudes and longitudes. I have asked this question earlier but couldn't get the appropriate answer to it. I have a service in the background which is supposed to keep track of the changing positions. I realised that the coordinates are not getting changed or they are set to null. I have tried different suggestions but the same problem persists. Please can somebody explain me step by step what to do. I have been sitting this for the past one week. My error logs and codes are as follows:

Error logs

 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)

And the code of my Service :

 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);

}

}

}

网友答案:

I have a similar app, which uses a service to retrieve locations. In the oncreate method initialise google services and the location location manager like so:

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);
    }
}

Once initialised, then your onLocationChanged callback will be hit when you location is acquired and updated ever 20 seconds as set in the TIME_IN_MILLI variable.

分享给朋友:
您可能感兴趣的文章:
随机阅读: