隐藏应用程序时如何发送和接收数据?

时间:2015-11-02 18:22:08

标签: java android json android-asynctask httpurlconnection

当应用程序被隐藏或屏幕关闭时,我的应用程序不会向PHP脚本发送JSON字符串。我使用HttpURLConnection。我的应用程序发送GPS位置。我想,那个应用程序可以像垃圾一样在backgoround中工作。发送和接收数据发生在AsyncTask中。有什么问题?

public class MyAsyncTask extends AsyncTask<JSONObject, Void, JSONObject> {

    String addr = GlobalConfig.addr;
    String prot = GlobalConfig.prot;
    int port = GlobalConfig.port;

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected JSONObject doInBackground(JSONObject... params) {

        JSONObject json = params[0];
        String string = "json="+json;

        try {

            URL url = new URL(prot,addr,port,"json/myLocation.php");

            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setDoOutput(true);
            httpCon.setDoInput(true);
            httpCon.setUseCaches(false);
            httpCon.setConnectTimeout(15000);
            httpCon.setRequestProperty("Content-Length", Integer.toString(string.length()));
            httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpCon.setRequestMethod("POST");

            DataOutputStream wr = new DataOutputStream(httpCon.getOutputStream());
            wr.writeBytes(string);
            wr.flush();
            wr.close();

            int responseCode = httpCon.getResponseCode();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    @Override
    protected void onPostExecute(JSONObject json) {

    }

}


public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;

    boolean isGPSEnabled = false;

    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    Location location;
    double latitude;
    double longitude;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

    private static final long MIN_TIME_BW_UPDATES = 10000;

    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {

            } else {
                this.canGetLocation = true;

                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }

                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }


    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }
    }


    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        return latitude;
    }


    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        return longitude;
    }


    public boolean canGetLocation() {
        return this.canGetLocation;
    }


    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        alertDialog.setTitle("GPS is settings");

        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {

        int ID = GlobalConfig.ID;
        int Random = GlobalConfig.Random;

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        try {

            JSONObject json = new JSONObject();
            json.put("ID", ID);
            json.put("Random", Random);
            json.put("latitude", latitude);
            json.put("longitude", longitude);

            new MyAsyncTask().execute(json);

        } catch(Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

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

}

喜欢这个吗?

3 个答案:

答案 0 :(得分:1)

我认为你在混合不同的东西:

AsyncTask意味着代码在一个单独的任务(线程)中运行,但仍然在您的活动的上下文中。这意味着它在您的应用程序的后台运行,并且不会停止执行您的应用程序。

服务能够在后台执行代码而无需任何活动上下文。这就像在系统的背景中。

要实现您的目标,您必须将您的任务放入服务中。

答案 1 :(得分:0)

就像Christian说的那样,如果你想在应用程序处于后台时更新坐标,你将需要一项服务。

该服务将实现位置监听器,当位置更新时,您将运行asynctask。

答案 2 :(得分:0)

你需要综合各种方法来实现这一目标。

首先,您需要了解如何使您的服务变得粘性。为此,请按照此处的步骤操作:

Android Service needs to run always (Never pause or stop)

粘性服务是一种运行的服务,直到您明确告诉它关闭(http://developer.android.com/reference/android/app/Service.html#START_STICKY

您还希望根据文档https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

查看wakefulBroadcastReceiver

这样,当设备重新启动或关机后启动时,它会再次启动您的服务。

为了更加安全,您还需要了解实现某种形式的network broadcast receiver,例如在此处实施解决方案:Broadcast receiver for checking internet connection in android app

最后一个是在没有网络连接时不尝试发送内容,而是在设备重新联机时启动服务。

但我建议你谨慎行事。你总是在运行,总是上传服务会消耗很多权力,也许你想限制它发送的数据量和收集这些数据的时间间隔,但这取决于你是否倾听:)。