2010-12-07 10 views
1

私はLocation APIを大量に使用しようとしています。 TextListをLocationListenerでリフレッシュする単純なアクティビティを実行しても問題はありません。これは問題ありません。Androidの場所とスレッド

主なアクティビティから呼び出されるマネージャクラスのようなものに、すべての場所の情報を入れる必要があります。だから私はそのクラスの中にすべてを配置し、アクティビティに場所を伝える必要があります。これは活動です:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main); 
    g = new GPSManager(getApplicationContext()); 
    t = new Thread(g); 
    t.start(); 
    updateWithNewLocation(g.getLocation()); 
}  
private void updateWithNewLocation(Location location) { 
    String latLongString; 
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.loca); 
    if (location != null) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     latLongString = "Lat:" + lat + "\nLong:" + lng; 
    } else { 
     latLongString = "No location found"; 
    } 
    myLocationText.setText("Your Current Position is:\n" + 
    latLongString); 
}  

そして、これは「マネージャ」クラスです:

public GPSManager(Context context) { 
    locationManager = (LocationManager)context.getSystemService(service); 
    String service = Context.LOCATION_SERVICE; 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setAltitudeRequired(false); 
    criteria.setBearingRequired(false); 
    criteria.setCostAllowed(true); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    provider = locationManager.getBestProvider(criteria, true); 
} 
public Location getLocation() { 
    return this.location; 
} 
private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     Manager.this.location=location; 
    } 
    public void onProviderDisabled(String provider){ 
     locationManager.removeUpdates(this); 
    } 
    public void onProviderEnabled(String provider){ } 
    public void onStatusChanged(String provider, int status, 
     Bundle extras){ } 
    }; 

@Override 
public void run() { 
    Looper.prepare(); 
    locationManager.requestLocationUpdates(provider, 0, 0, 
      locationListener);   
    location = locationManager.getLastKnownLocation(provider); 
    Looper.loop(); 
    Looper.myLooper().quit(); 
} 
private Handler handler = new Handler() { 
@Override 
public void handleMessage(Message msg) { 
    locationManager.removeUpdates(locationListener); 
} 

私が持っている問題はよく、それがすべてで動作しますしない、ということです!このコードをアクティビティに入れても、スレッドは関与していません。うまくいきます。しかし、この方法では、「場所が見つかりません」と表示されます。私はマネージャクラスを常に変更を聞くようにする必要があるので、私は現在の場所が更新されることを頼むときに。

答えて

0

オプション#1:「マネージャ」クラスを削除します。

オプション#2:「マネージャ」クラスをServiceにします。

オプション3:「マネージャ」クラスをカスタムApplicationにします。

オプション#4:ThreadHandlerThreadに置き換えてください。

+0

お返事ありがとうございます!マネージャークラスを削除することは間違いなく選択肢ではありませんが、ServiceまたはApplicationにすることはできません。そのため、HandlerThreadを作成することが最適なソリューションになります。問題は、私はそれを実装する方法に関する情報を見つけることができないということです。私に余分な手を差し伸べることはできますか?ありがとう:) – ferostar

+0

@Peter Canthropus:私は決して 'HandlerThread'を使用していません。 IMHOという4つのオプションのうち、最悪のものです。私が推測しているのは、これを「マネージャー」に引っ張ったのは、それが複数のアクティビティにまたがって働くようにするためです。その場合、いくつかのスレッドをフォークするのは非常に悪い考えです。あなたのマネージャー*は、Androidアプリケーションのライフサイクルを認識しているコンポーネントによって "管理"される必要があるため、アプリケーションがフォアグラウンドにないときにこのスレッドをきれいにシャットダウンすることができます。 – CommonsWare

0

私は、LocationListenerと自分のThreads/HandlerThreadsを混在させたロケーションアップデートを受け取れなかったのと同じ経験をしました。解決策はPendingIntentを使用することでした:How to receive location updates without using a service