0

ユーザーの位置情報を 5 分ごとに更新する Android サービスを作成しようとしています。DDMS を使用して、正常に動作するエミュレータに座標を送信しています。これらの座標を変換して場所を取得する必要があります。例: ニューヨークとトーストを使用して画面に印刷します。私はジオコーダーを使用してDDMSから提供された座標を場所に変換しようとしているマップを使用していません。エラーは発生しませんが、座標が場所に変換されておらず、画面に何も表示されていないようです。非常に長い間これに苦労していたので、助けてください。これが私のコードです。ありがとう

public class GetLocationService extends Service {
protected LocationManager locationManager;
Button start;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocationListener ll = new MyLocListener();
    Location location = new Location("abc");
    ll.onLocationChanged(location ); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
    return START_STICKY;
}

private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
     if (location != null) {
    Log.d("LOCATION CHANGED", location.getLatitude() + "");
    Log.d("LOCATION CHANGED", location.getLongitude() + "");
    }
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Toast.makeText(getApplicationContext(),
    location.getLatitude() + "" + location.getLongitude(),
    Toast.LENGTH_LONG).show();
    try{
         Geocoder geo = new Geocoder(GetLocationService.this.getApplicationContext(), Locale.getDefault());
         List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
             if (addresses.size() > 0) {       
                Log.d("TAG",addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName());
             }
             Toast.makeText(getApplicationContext(),
                     addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName(),
                        Toast.LENGTH_LONG).show();
          }

        catch (Exception e) {
            e.printStackTrace(); 
        }
        }
     }
   }

////サービス開始////

 public class MyService extends Activity {

 @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service);
    startService(new Intent(getBaseContext(), GetLocationService.class));

  }
}

ここに画像の説明を入力

4

1 に答える 1

0

よし、Log ステートメントは届いているか? また、トーストは表示されません。これは、渡されたコンテキストがサービス コンテキストであるためです。サービスはユーザー インターフェイスの一部ではないため、サービス コンテキストは表示されません。アプリケーション コンテキストは機能する可能性がありますが、実際には現在のアクティビティのコンテキストを使用する必要があります。

于 2012-07-10T18:02:57.840 に答える