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

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

问题描述:

当应用程序被隐藏或屏幕关闭时,我的应用程序不会向PHP脚本发送JSON字符串。我使用HttpURLConnection。我的应用程序发送一个GPS位置。我想,该应用程序像messanger一样在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; 
    } 

} 

是这样的吗?

+0

请发布您的代码并清除您的问题更多 – therealprashant

我认为你是混合不同的事情:

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

服务能够在没有任何活动上下文的情况下在后台执行代码。这就像系统的背景一样。

为了实现你想要的,你必须把你的任务放在一个服务中。

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

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

+0

我更新了我的帖子。我的代码包含一个您正在编写的服务,但它不会发送数据。 GPSTracker是一个服务。当位置改变时,JSONObject被转移到AsyncTask。 – SeaDog

+0

@SeaDog是否将服务添加到清单文件中? – meda

+0

我忘了它,但如果我的代码是正确的? – SeaDog

您需要结合使用这些工作。

首先,您需要查看使您的服务变得粘滞。要做到这一点,按照下列步骤进行:

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

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

您也想看看wakefulBroadcastReceiver服务为每该文档:https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

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

为了更加安全,您还需要看看这里通过实施这一方案实施某种形式的network broadcast receiver,例如:Broadcast receiver for checking internet connection in android app

这最后一个是这样,你不尝试发送的东西当没有网络连接时,而是在设备重新联机时启动服务。

但是,我确实建议您对此谨慎行事。你一直在运行,总是上传服务将消耗大量的权力,也许你想限制它发送的数据量和它收集这些数据的时间间隔,但这取决于你听或不:)。