Android 如何在手机屏幕关闭的情况下保持GPS和插座连接?

Android 如何在手机屏幕关闭的情况下保持GPS和插座连接?,android,gps,wakelock,Android,Gps,Wakelock,问题:我必须定期将GPS数据发送到服务器 方法:我使用了运行后台服务的alarm manager。它获取GPS数据并通过插座发送。在屏幕打开之前,它工作正常 问题:但是,当屏幕关闭时,服务器上不会收到任何数据。但只要我打开屏幕,服务器就会立即接收所有数据 已尝试:我已尝试wakelock,但仍面临相同的问题 请帮我解决这个问题 提前谢谢 public class service_task extends Service { public static int PORT = 6789;

问题:我必须定期将GPS数据发送到服务器

方法:我使用了运行后台服务的alarm manager。它获取GPS数据并通过插座发送。在屏幕打开之前,它工作正常

问题:但是,当屏幕关闭时,服务器上不会收到任何数据。但只要我打开屏幕,服务器就会立即接收所有数据

已尝试:我已尝试wakelock,但仍面临相同的问题

请帮我解决这个问题

提前谢谢

public class service_task extends Service {

    public static int PORT = 6789;
    public static String IP = "192.168.20.16";
    public PowerManager.WakeLock wl;

    @SuppressLint({"MissingPermission", "InvalidWakeLockTag"})
    @Override
    public void onCreate() {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::service_task");
        wl.setReferenceCounted(true);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    private class ServerThread extends Thread implements LocationListener {

        public LocationManager locationManager = null;
        public String msg = "default";
        public String id = "default";
        private Location mLocation = null;
        public Socket socket = null;
        public int serviceid;
        public LocationListener LL;


        public ServerThread(LocationManager locationManager, String id, int serviceid) {
            super("UploaderService-Uploader");
            this.locationManager = locationManager;
            this.id=id;
            this.serviceid=serviceid;
            LL=this;
        }

        @SuppressLint("MissingPermission")
        public void run() {

            wl.acquire();
            Looper.prepare();
            Log.d("func", "Looper");
            this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            Handler handler=new Handler(Looper.myLooper());
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    Log.d("func", "no GPS data found");
                    locationManager.removeUpdates(LL);
                    Looper.myLooper().quit();
                }
            };
            handler.postDelayed(runnable, 30000);
            Looper.loop();

            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String cal = sdf.format(calendar.getTime());

            if (mLocation!=null) {
                msg = id + "    time:" + cal + "    GPS data:" + mLocation;
            }else{
                msg = id + "    time:" + cal + "    No GPS data";
            }
            Log.d("D", msg);
            socket = null;
            SocketAddress address = new InetSocketAddress(IP, PORT);
            socket = new Socket();
            try {
                Log.d("D", "server connected");
                socket.connect(address, 3000);
            } catch (IOException e) {
                Log.d("D","server not connected");
                e.printStackTrace();
            }
            try {
                socket.setSoTimeout(3000);
            } catch (SocketException e) {
                Log.d("socket timeout","server took too long to respond");
                //e.printStackTrace();
            }

            OutputStream out = null;
            try {
                out = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            PrintWriter output = new PrintWriter(out);
            output.print(msg);
            output.flush();
            output.close();
            try {
                socket.close();
                Log.d("D", "server disconnected for "+id);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d("D", "Stopped for "+id);

            wl.release();
            stopSelf(serviceid);
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d("func", "location changed");
            mLocation = location;
            this.locationManager.removeUpdates(this);
            Looper.myLooper().quit();
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }

    }

    @SuppressLint("MissingPermission")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Date currentTime = Calendar.getInstance().getTime();
        IP= intent.getStringExtra("IP");
        PORT= Integer.parseInt(intent.getStringExtra("port"));
        String id= intent.getStringExtra("ID");
        Log.d("func", currentTime+" Started for "+id);
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        ServerThread thread = new ServerThread(locationManager, id, startId);
        thread.start();
        return START_STICKY;
    }
}

当手机屏幕关闭时,若设备中并没有挂起的唤醒锁,则设备CPU将关闭,若设备进入睡眠模式,则设备中将有进一步的限制。Android最新版本对应用程序背景行为和位置更新有限制。在代码中,您使用的是不推荐版本的位置更新方法。因此,您报告的行为是预期的

有一个关于位置更新的谷歌代码实验室教程,建议一致接收位置更新的正确方法,请遵循

谢谢你的回答。但融合定位客户端结合不同的信号来提供位置信息。我特别想使用内置GPS模块获取位置。有没有一种方法可以让我继续使用GPS_提供商的位置管理器?有人能帮我吗?我有点困了。