【问题标题】:Why does background running service cannot get and/or update the locations?为什么后台运行服务无法获取和/或更新位置?
【发布时间】:2014-03-22 01:01:38
【问题描述】:

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

错误日志

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

【问题讨论】:

    标签: android service locationmanager forceclose


    【解决方案1】:

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

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

    一旦初始化,您的 onLocationChanged 回调将在您获取位置时触发,并按照 TIME_IN_MILLI 变量中的设置每 20 秒更新一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      相关资源
      最近更新 更多