1

GPS.javaというサービスにバインドされているmessage.javaというアクティビティがあります。 GPS.javaから取得した緯度と経度は、message.javaで使用する必要があります。 servicebinderを使用して、onServiceConnected()で緯度と経度のデフォルト値を取得しています。 ここmessage.javaサービス完了時にアクティビティを通知するにはどうすればよいですか?

`Intent i=new Intent(getApplicationContext(), GPS.class); 
      bindService(i,mConnection,Context.BIND_AUTO_CREATE) 




     private GPS servicebinder; 
     ServiceConnection mConnection = new ServiceConnection() { 
      public void onServiceConnected(ComponentName className, IBinder service) { 
       servicebinder = ((GPS.MyBinder)service).getService(); 

       double lat=servicebinder.getlatitude(); 

       double lon=servicebinder.getlongitude(); 
        tex.setText("\nlatitude: " + lat+ "\nlongitude: " 
         + lon); 


       // do any extra setup that requires your Service 
       } 

getLatitude()とgetLongitude()正しいGPS.javaに見出さ緯度と経度を返すためのコードです。問題は上記のlatとlonがデフォルト値を表示しているので、サービスの緯度と経度が更新されたらlatとlonも更新する必要があります(サービスの更新時にアクティビティに通知する必要があります)

適切

答えて

0

まず

public interface MyLocationListener { 
    public void locationChanged(double lat, double lon); 
} 

今すぐ

public class GPS { 
    ArrayList<MyLocationListener> listeners = new ArrayList<MyLocationListener>(); 

    public void addLocationListener(MyLocationListener listener) { 
    listeners.add(listener); 
    } 
} 
として GPSクラスを更新し、次のよう MyLocationListenerのようなインターフェイスを作成し、それらを配置するためのコード

だから、あなただけの

notifyChangeLocation(lat, lon); 

を呼び出し、このメソッドは、コードを次している緯度や経度を変更した場合は:

public void notifyChangeLocation(double lat, double lon) { 
Iterator<MyLocationListener> itr = listeners.iterator(); 
    while(itr.hasNext()) { 
    itr.next().locationChanged(lat, lon); 
    } 
} 

は、これが最初の部分で、今2つ目は追加することです今だけ聞く登録

public class MyServiceConnection implements ServiceConnection, MyLocationListener { 
    //add the unimplemented methods 
    public void locationChanged(double lat, double lon) { 
    // do any extra setup that requires your Service 
    } 
} 
ServiceConnection mConnection = new MyServiceConnection(); 
Intent i=new Intent(getApplicationContext(), GPS.class); 
bindService(i,mConnection,Context.BIND_AUTO_CREATE); 

:あなたの活動では、似ているクラスMyServiceConnectionを行うことによって、リスナーer gps.addLocationListener(mConnection);

関連する問題