私のアプリケーションでは、ユーザーがブロードキャストの助けを借りてアプリケーションを閉じた場合、一定時間後にユーザーの位置を見つけようとしています。
ただし、ブロードキャストレシーバークラスでは、ユーザーの場所の希望する緯度と経度を取得しています。しかし、その緯度と経度を使用して、ユーザーの住所を見つけるにはどうすればよいでしょうか。
ジオコーダーにはアクティビティ コンテキストが必要ですが、ブロードキャスト レシーバーを拡張するクラスがあります。
public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b=intent.getExtras();
Location loc=(Location)b.get(LocationPoller.EXTRA_LOCATION);
// Getting latitude
double latitude = loc.getLatitude();
// Getting longitude
double longitude = loc.getLongitude();
System.out.println( loc.getLatitude());
System.out.println( loc.getLongitude());
addGet(latitude,longitude)
}
public void addGet(double latitude,double longitude) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
Log.e("Addresses","-->"+addresses);
}
catch (IOException e)
{
e.printStackTrace();
}
}