2012-01-11 15 views
0

これまで準備していたことを共有し、同時に必要な次のコーディング手順について助言と提案を求めます。短いと基本的なJavaとAndroidのトレーニングやオンラインリソースから基づいandroid gpsクライアントは、地域内の場合にのみサーバーに座標を送信します。

(私はまだそれをテストしていないので、理論的に)、私は次のような目的を持って、次の理論上のコードが出ている:

  1. 明示的に
  2. は、3Gを介してサーバに接続のTextViewに携帯電話の位置を知ること
  3. dislays現在位置をGPSプロバイダ(GPS /セル/無線LAN)を選択
  4. はにできるだけ頻繁緯度、経度、タイムスタンプを送ります。サーバー以下は

私が用意したコードです:

import android.app.Activity; 
import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.TextView; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

public class GpsActivity extends Activity { 

private LocationManager lm; 
private LocationListener locationListener; 
public static TelephonyManager tm; 
public static TextView tv; 
public static Socket s; 
public static PrintWriter out; 


/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
/** 
    * retrieve a reference to provide access to information about the telephony services on the device  
    */ 
    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    setContentView(R.layout.main); 
/** 
    * retrieve a reference to provide access to the system location services  
    */    
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  


/** 
* explicitly select the GPS provider, create a set of Criteria and let android choose the best provider available 
*/ 

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setCostAllowed(true); 
criteria.setPowerRequirement(Criteria.POWER_LOW); 
String provider = lm.getBestProvider(criteria, true); 
/** 
* This method takes in four parameters: 
provider: The name of the provider with which you register 
minTime: The minimum time interval for notifications, in milliseconds. 
minDistance: The minimum distance interval for notifications, in meters. 
listener: An object whose onLocationChanged() method will be called for each location update. 
*/ 
locationListener = new MyLocationListener(); 
lm.requestLocationUpdates(provider, 0, 0, locationListener); 

tv = (TextView) findViewById(R.id.textView1); 
tv.setText("I currently have no Location Data."); 

} 

/** 
* Connects the Android Client to a given server 
* 
* @param name 
*   The name of the remote server 
* @param port 
*   Port number to connect to at the remote server. 
* @throws IOException 
* @throws UnknownHostException 
*/ 
public static void connect(String name, int port) 
     throws UnknownHostException, IOException 
{ 

    s = new Socket(name, port); 
    out = new PrintWriter(s.getOutputStream(), true); 
} 

/** 
* Sends a string message to the server. 
* 
* @param msg 
*   The message to be sent. 
* @throws IOException 
*/ 
public static void send(String msg) throws IOException 
{ 
    if (!s.isClosed() && msg != null) 
    { 
     out.println(msg); 
     if (msg.contains("CMD_QUIT")) 
     { 
      out.close(); 
      s.close(); 
      Log.i("ServerConnection", "Client Disconnected."); 
     } 
    } 
} 


private class MyLocationListener implements LocationListener{ 

    @Override 
    public void onLocationChanged(Location loc) { 
     String txt = "Latitude:" + loc.getLatitude() + "/nLongitude:" + loc.getLongitude(); 
     Log.i("GeoLocation", "My current location is:\n " + txt); 
     tv.setText("My current location is:\n" + txt); 
     String msg = loc.getLongitude() + "\n" + loc.getLatitude() + "\n" 
      + loc.getTime(); 

    try 
     { 
     connect("IP address", 27960); 
     send("CMD_HELLO"); 
     send(msg); 
     send("CMD_QUIT"); 
     } catch (UnknownHostException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 



    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 

}

を上記のコードは、与えられた4つの目標を達成するかどう

  1. が親切にコメントを助けてください。
  2. 私の5番目の目標を達成する方法-----私はアンドロイドアプリケーションがサーバへの接続を開始し、電話(車に使用されている)が道路エリア内にある場合のみ緯度と経度を送信するようにします1km×30mのエリア)。継続的にその位置を聞いていますが、エリアに入るとサーバーに送信を開始し、エリアを出ると一度だけ送信し、停止します。

答えて

0
  1. 号あなたは最高のプロバイダを提供するために、OSを尋ねます。これは、リスナーからのテキストビューを更新できないので、lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  2. は機能しません。 android.OS.Handler
  3. 技術的にはこれを実現しますが、独自のプロトコルを開発するのではなく、HTTP経由でJSONやXMLを使う方がよいでしょう。 5については
  4. はい

、2つの場所、北西の場所とあなたのボックスを表す東南アジアの場所を作成します。あなたonLocationChanged方法では、コーナーと新しい位置を比較するように(> l.lat & & nw.lat < l.lat se.lat)及び(l.lon < se.lon & & l.lon> NW。 lon) "l"はコールバックからの最新の場所、 "se"は境界の南東の角、 "nw"はバウンダの北西の角です。上記の4つの条件を満たす場合は、サーバーに送信します。

+0

1.ありがとうuser931366!あなたの時間、コメント、および助けに感謝します。あなたは正しいです。私は私が意味するものなので、 "明示的に最高のプロバイダ(GPS /携帯/無線LAN)を選択する必要があると思う"と思う。 2.確認します。3. JSONやXMLに精通していません。あなたの提案をチェックします。4.クール5.多くの意味を理解してください。 – coollearner

+0

こんにちはuser931366。私は、矩形のGPSエリアの辺が緯度と経線に平行であれば、目的5の答えはOKだと思います。矩形領域の辺が緯度と経線に平行でない場合はどうなりますか? – coollearner

関連する問題