2015-11-02 122 views
0

當應用程序被隱藏或屏幕關閉時,我的應用程序不會向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

回答

1

我認爲你是混合不同的事情:

的AsyncTask意味着代碼在一個單獨的任務(線程)運行,但仍然在你的活動環境。這意味着它運行在你的應用程序的後臺,並不會停止你的應用程序的執行。

服務能夠在沒有任何活動上下文的情況下在後臺執行代碼。這就像系統的背景一樣。

爲了實現你想要的,你必須把你的任務放在一個服務中。

0

就像基督教說的那樣,如果你想在應用程序處於後臺時更新座標,你將需要一個服務。

服務將實現位置偵聽器,當位置更新時,您將運行asynctask。

+0

我更新了我的帖子。我的代碼包含一個您正在編寫的服務,但它不會發送數據。 GPSTracker是一個服務。當位置改變時,JSONObject被轉移到AsyncTask。 – SeaDog

+0

@SeaDog是否將服務添加到清單文件中? – meda

+0

我忘了它,但如果我的代碼是正確的? – SeaDog

0

您需要結合使用這些工作。

首先,您需要查看使您的服務變得粘滯。要做到這一點,按照下列步驟進行:

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

這最後一個是這樣,你不嘗試發送的東西當沒有網絡連接時,而是在設備重新聯機時啓動服務。

但是,我確實建議您對此謹慎行事。你一直在運行,總是上傳服務將消耗大量的權力,也許你想限制它發送的數據量和它收集這些數據的時間間隔,但這取決於你聽或不:)。

相關問題