Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LocationListener没有调用onLocationChanged_Android_Location_Listener_Android Location - Fatal编程技术网

Android LocationListener没有调用onLocationChanged

Android LocationListener没有调用onLocationChanged,android,location,listener,android-location,Android,Location,Listener,Android Location,从这次活动我打电话给服务部 但是,只要通过滑动从最近的应用程序中删除我的应用程序,位置侦听器就会停止工作,并且不会显示任何位置更改更新,但是如果应用程序正在运行,那么它就可以正常工作 public class LocationService extends Service{ private LocationManager locationMangaer=null; private LocationListener locationListener=null; p

从这次活动我打电话给服务部 但是,只要通过滑动从最近的应用程序中删除我的应用程序,位置侦听器就会停止工作,并且不会显示任何位置更改更新,但是如果应用程序正在运行,那么它就可以正常工作

public class LocationService extends Service{

    private LocationManager locationMangaer=null;
    private LocationListener locationListener=null; 



    private static final String TAG = "Debug";
    private Boolean flag = false;
    public static String cityName=null;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


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


        Log.d("Location Serive","Started");
        locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();

        flag = displayGpsStatus();
        boolean c=isOnline();
        if(flag)
        {
            locationMangaer.removeUpdates(locationListener);
            Criteria crit2 = new Criteria();
            crit2.setAccuracy(Criteria.ACCURACY_FINE);
            String provider2 = locationMangaer.getBestProvider(crit2, false);
            Toast.makeText(getApplicationContext(), provider2, Toast.LENGTH_LONG).show();
            locationMangaer.requestLocationUpdates(provider2,1, 1,locationListener);

        }
        else if(c)
        {
            locationMangaer.removeUpdates(locationListener);
            Criteria crit = new Criteria();
            crit.setPowerRequirement(Criteria.POWER_LOW);
            crit.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = locationMangaer.getBestProvider(crit, false); 
            Toast.makeText(getApplicationContext(), provider, Toast.LENGTH_LONG).show();
            locationMangaer.requestLocationUpdates(provider,1, 1,locationListener);

        }
        else
        {
            //Nothing Available
        }
        Log.d("Internet",Boolean.toString(c));

    }




    /*net Enabled or Not*/
    public boolean isOnline() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }
    /*----------Method to Check GPS is enable or disable ------------- */
    @SuppressWarnings("deprecation")
    private Boolean displayGpsStatus() {
        ContentResolver contentResolver = getBaseContext().getContentResolver();
        boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);

        if (gpsStatus) {
            return true;

        } else {
            return false;
        }
    }



    /*----------Listener class to get coordinates ------------- */
    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
          Log.d("Location Changed", "location:");
        Log.d("Latitude", Double.toString(loc.getLatitude()));
        Log.d("Longitude",Double.toString(loc.getLongitude()));
        Toast.makeText(getApplicationContext(), Double.toString(loc.getLatitude()), Toast.LENGTH_LONG).show();
        double p=loc.getLongitude();
        double q=loc.getLatitude();

        LocationChecker(p,q);


        }

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

        }

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

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }




    }

您是否在活动结束时停止服务

您还可以重写方法并使用值

更新1

您可以在单独的流程中启动服务。见文件:

如果进程名称以小写字符开头,则服务 将在该名称的全局进程中运行,前提是 允许这样做

例如:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

不,我没有停止我的服务,我也尝试过,该服务正在运行,但位置侦听器停止提供任何更新top提供任何更新,因为logcat中没有显示纬度和经度的日志,或者没有显示toast,或者两者都没有?不,实际上,当应用程序在后台时,它可以进行位置更改并显示toast或log,但当我关闭应用程序服务的gps图标被关闭,在我再次启动活动之前,它不会进行任何更新。如何关闭应用程序?这样做会终止所有应用程序进程,因此服务被终止,我将更新我的答案
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
    <service
        android:name=".LocationService"
        android:process="myLocationService" [...] >
        [...]
    </service>