gps ポイントをサーバーに送信するシングルトン スレッド クラスがあります。このクラスは、GPS 位置を保持して更新します。
ファインダスレッド:
public class FinderThread extends Thread {
private static FinderThread SINGLETON;
public boolean isinterrupt = true;
public int maxage;
public int minage;
public int distance;
public String gender;
public String latitude;
public String longitude;
public String sid;
public Context ctx;
LocationManager gps;
boolean nav;
SharedPreferences pref;
private Handler dlh;
private Handler myLocationHandler;
boolean pause=false;
private FinderThread(Context ctx,Handler dlh, Handler myLocationHandler) {
this.ctx = ctx;
gps = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
gps.requestLocationUpdates( LocationManager.GPS_PROVIDER,60000, 20, llistener);
pref = ctx.getSharedPreferences("NFF", 0);
sid = pref.getString("sid", "");
this.dlh = dlh;
if (myLocationHandler!=null)
{
this.myLocationHandler = myLocationHandler;
}
if(myLocationHandler != null)
{
myLocationHandler.sendEmptyMessage(0);
}
}
public static synchronized FinderThread getInstance(Context ctx,Handler dlh, Handler myLocationHandler)
{
if (SINGLETON == null) // falls null
{
SINGLETON = new FinderThread (ctx,dlh,myLocationHandler);//instanzieren
}
else
{
SINGLETON.myLocationHandler= myLocationHandler;
SINGLETON.dlh = dlh;
myLocationHandler.sendEmptyMessage(0);
}
return SINGLETON;
}
/*
@Override
public synchronized void start() {
if(SINGLETON != null)
{
SINGLETON.start();
}
}
public synchronized void pause()
{
if(SINGLETON.getState()==State.RUNNABLE || SINGLETON.getState()==State.WAITING)
{
SINGLETON.pause = true;
}
}
public synchronized void tcontinue ()
{
if(SINGLETON.getState()==State.RUNNABLE || SINGLETON.getState()==State.WAITING)
{
SINGLETON.pause = false;
}
}
public synchronized boolean isPaused()
{
return SINGLETON.pause;
}
*/
public synchronized void pause()
{
this.pause = true;
}
public synchronized boolean isPaused()
{
return this.pause;
}
public synchronized void tcontinue ()
{
this.pause = false;
}
@Override
public void run() {
//String locationProvider = LocationManager.GPS_PROVIDER;
//Location lastpoint = gps.getLastKnownLocation(locationProvider);
//longitude = String.valueOf(lastpoint.getLongitude());
//latitude = String.valueOf(lastpoint.getLatitude());
try
{
while (isinterrupt) {
if(!pause)
{
Log.e("NFF", "Finder Thread Begin");
RestConnection r = new RestConnection("finder", dlh, "POST");
r.setParameter("minage", String.valueOf(minage));
r.setParameter("maxage", String.valueOf(maxage));
r.setParameter("distance", String.valueOf(distance));
r.setParameter("gender", String.valueOf(gender));
r.setParameter("latitude", String.valueOf(latitude));
r.setParameter("longitude", String.valueOf(longitude));
r.setParameter("sid", sid);
Log.e("NFF Finder Paramater", String.valueOf(minage)+" "+String.valueOf(maxage)+" "+String.valueOf(distance)+" "+String.valueOf(gender) + " "+String.valueOf(latitude)+" " +String.valueOf(longitude)+ " " + sid );
r.start();
}
Thread.sleep(10000);
}
}
catch (InterruptedException e) {
Log.e("NFF FT", "InterruptedException");
isinterrupt = false;
}
}
private LocationListener llistener = new LocationListener (){
@Override
public void onLocationChanged(Location location) {
if(String.valueOf(location.getLatitude())!= null)
{
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
if(myLocationHandler != null)
{
myLocationHandler.sendEmptyMessage(0);
}
}
}
@Override
public void onProviderDisabled(String provider) {
Log.e("NFF", "GPS Disabled");
isinterrupt = true;
}
@Override
public void onProviderEnabled(String provider) {
Log.e("NFF", "GPS Enabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
public void saveProperties ()
{
Editor editor = pref.edit();
editor.putInt("maxage", maxage);
editor.putInt("minage", minage);
editor.putInt("distance", distance);
editor.putString("gender", gender);
editor.putString("lat", latitude);
editor.putString("long", longitude);
editor.commit();
}
public void loadProperties ()
{
maxage = pref.getInt("maxage", -1);
minage = pref.getInt("minage", -1);
distance = pref.getInt("distance", -1);
gender = pref.getString("gender", "f");
latitude = pref.getString("lat", "f");
longitude = pref.getString("long", "f");
}
public synchronized void pushSettings()
{
SINGLETON.maxage = maxage;
SINGLETON.minage = minage;
SINGLETON.distance = distance;
SINGLETON.gender = gender;
SINGLETON.latitude = latitude;
SINGLETON.longitude = longitude;
}
}
エミュレーターでアプリをテストし、エミュレーター コントロールで位置を設定すると、すべて正常に動作します。携帯電話でテストすると、ドロイドは位置を見つけられません。私はアプリ「GPS Test」をテストしました.5つの衛星で3分以内に位置を見つけます.
助けてください