Android 在手机启动后获取 GPS 位置,我试图将其获取到服务中,但位置返回为 null

发布于 2024-11-18 10:03:28 字数 2088 浏览 5 评论 0原文

这是代码。

我如何确定该位置始终返回而不是 null。

package com.test.location;
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.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
 public class NotifyOnBoot extends Service  {
        Location location;

        @Override
        public IBinder onBind(Intent arg0) {
         return null;
        }

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

                     updateNotification();

                     Thread th = new Thread(null, task, "myService");
                     th.start();
         }

        private Runnable task = new Runnable() {
         public void run() {

            if(location == null)
            {
                Log.d("location","No location");
            }
            else
                Log.d("location",location.toString());


            NotifyOnBoot.this.stopSelf();
         }
     };

     @Override
     public void onDestroy() {
         super.onDestroy();
     }

     @Override
     public int  onStartCommand  (Intent  intent, int flags, int startId){
            return START_STICKY;
     }


     protected void updateNotification()
     {
         LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         LocationListener locationListener = new MyLocationlistener();
         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
         location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     }
     private class MyLocationlistener implements LocationListener{
         public void onLocationChanged(Location location){}
         public void onProviderDisabled(String provider){}
         public void onProviderEnabled(String provider){}
         public void onStatusChanged(String provider, int status, Bundle extras){}
     }
} 

Here is the code.

How can I be sure, that location is returned alwasys instead of null.

package com.test.location;
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.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
 public class NotifyOnBoot extends Service  {
        Location location;

        @Override
        public IBinder onBind(Intent arg0) {
         return null;
        }

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

                     updateNotification();

                     Thread th = new Thread(null, task, "myService");
                     th.start();
         }

        private Runnable task = new Runnable() {
         public void run() {

            if(location == null)
            {
                Log.d("location","No location");
            }
            else
                Log.d("location",location.toString());


            NotifyOnBoot.this.stopSelf();
         }
     };

     @Override
     public void onDestroy() {
         super.onDestroy();
     }

     @Override
     public int  onStartCommand  (Intent  intent, int flags, int startId){
            return START_STICKY;
     }


     protected void updateNotification()
     {
         LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         LocationListener locationListener = new MyLocationlistener();
         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
         location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     }
     private class MyLocationlistener implements LocationListener{
         public void onLocationChanged(Location location){}
         public void onProviderDisabled(String provider){}
         public void onProviderEnabled(String provider){}
         public void onStatusChanged(String provider, int status, Bundle extras){}
     }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2024-11-25 10:03:28

我建议创建一个回调接口并使用 onLocationChanged ,它会在位置首次启动时通知回调。

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

I recommend to create a call back interface and use onLocationChanged which notifies that call back on the moment the location initiate for the first time.

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文