应用程序关闭时,服务中的WebService请求响应android

时间:2018-02-07 06:48:26

标签: java android asp.net web-services android-service

我的Android应用中有一个服务类。 此服务获取用户位置并使用 ASP WebService将位置发送到服务器。 以下是我的服务代码:

public class ServiceSendLocation extends Service {

    private static final String TAG = "ServiceSendLocation";

    final long milliseconds_sleep = 3000;
    private boolean isRunning = false;

    @Override
    public void onCreate() {
        super.onCreate();
        isRunning = true;
        Log.i(TAG, "Service onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, final int startId) {
        Log.i(TAG, "Service onStartCommand");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(milliseconds_sleep);
                    } catch (Exception e) {}
                    getLocationAndSend();

                    if (isRunning)
                        Log.i(TAG, "Service running");
                }
            }
        }
    }).start();

        return Service.START_STICKY;
}

    private void getLocationAndSend() {
        Log.i(TAG, "Service getLocationAndSend");
        LocationManager locationManager = null;
        LocationListener locationListener = new ServiceGetLocation();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "Service PERMISSION_GRANTED");
            try {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

                HelperWS.register(structCheckUser.getUserId(), ServiceGetLocation.lat + "", ServiceGetLocation.lng + "", new HelperWS.InsertGpsDriver() {
                    @Override
                    public void onSuccess(StructInsertGpsDriver success) {
                        Log.i(TAG, "Success : " +ServiceGetLocation.lat  + ", " + ServiceGetLocation.lng);
                        Toast.makeText(ServiceSendLocation.this, "success : " +ServiceGetLocation.lat  + ", " + ServiceGetLocation.lng , Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFail(String message) {
                        Log.i(TAG, "onFail");
                    }
                });

            } catch (Exception e) {}
        } else {
            Log.i(TAG, "Service PERMISSION_NOT_GRANTED");
        }
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        isRunning = false;
        Log.i(TAG, "Service onDestroy");
    }
}

并在清单中:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        ...>

        <service
            android:name=".SendLocation.ServiceSendLocation" />

    </application>

此代码将位置信息发送到服务器并从WebService获取响应。但是当应用程序关闭时,此代码会获取位置信息并发送请求,但我们不会获得webService响应

有人请帮助我。

0 个答案:

没有答案