2012-04-16 12 views
0

アンドロイドには若干の新サービスが必要です。インターバルXで現在の場所をポーリングするサービスがあります。そのサービスにバインドして、サービスからgetLastKnownLocationをアクティビティAに渡したいと思います。情報がバインドされたサービスからアクティビティにどのように渡されているか正確にはわかりませんそのバインダーまたは何を介して。とにかく、私はこれまでのところ私のコードです。サービスからの所在地の取得方法

サービス:

public class LocationService extends Service implements LocationListener { 


    LocationManager myLocationManager; 
    public Location myLocation; 
    LocationListener myLocationListener; 
    public static final String TAG = LocationService.class.getSimpleName(); 
    MyDB db; 
    double latitude,longitude; 
    Cursor c; 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Log.d(TAG, "service started (onCreate)"); 
     db = new MyDB(getApplicationContext()); 
     myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     Criteria criteria = new Criteria(); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     criteria.setAccuracy(Criteria.ACCURACY_LOW); 
     String locationProvider = myLocationManager.getBestProvider(criteria, true); 
     myLocationManager.requestLocationUpdates(locationProvider, 1000*60*2, 100, this); 
     myLocation = myLocationManager.getLastKnownLocation(locationProvider); 


    } 
public class MyBinder extends Binder { 
      LocationService getService() { 
       return LocationService.this; 
      } 
     } 

活動A:

public class myActivity extends Activity { 
    LocationManager myLocationManager; 
    Location myLocation; 

    boolean isBound = false; 

    private LocationService mBoundService; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     bindLocationService(); 

} 
private void bindLocationService() { 
     try { 
      isBound = getApplicationContext().bindService(new Intent(getApplicationContext(), LocationService.class), mConnection, BIND_AUTO_CREATE); 
      bindService(new Intent(this, LocationService.class), mConnection, BIND_AUTO_CREATE); 
     } catch (SecurityException e) { 
      // TODO: handle exception 
     } 
    } 
private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 
      mBoundService = ((LocationService.MyBinder)service).getService(); 
      Log.d(LocationService.TAG, "activity bound to service"); 

     } 

     public void onServiceDisconnected(ComponentName className) { 

      mBoundService = null; 
      Log.d(LocationService.TAG, "activity unbound to service"); 
     } 
    }; 
} 
+0

ロケーションを取得するにはLocationListenerを実装してリスニングを開始するだけです。これは別のスレッドで実行されます。 yway。ここでサービスを使用する特別な理由はありますか? –

答えて

3

このようなあなたのサービスからブロードキャストを送信する:

Intent i = new Intent(NEW_MESSAGE); 
Bundle bundle = new Bundle(); 
bundle.putString("yourvalue", value); 
i.putExtras(bundle); 
sendBroadcast(i); 

と、このようなあなたの活動に受信機に登録:

newMessage messageReceiver = new newMessage(); 
registerReceiver(messageReceiver, new IntentFilter(NEW_MESSAGE)); 

これはあなたの活動のクラスで、あなたの受信機である:

public class newMessage extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    {  
     String action = intent.getAction(); 
     if(action.equalsIgnoreCase(IMService.NEW_MESSAGE)){  
     Bundle extra = intent.getExtras(); 
     String username = extra.getString("yourvalue"); 
    } 
} 
2

のためのサービスとあなたの活動の間のコミュニケーションを、あなたの活動を更新するたびにカスタムBroadcastReceiverを作成し、サービスからそれを放送する意図を持っていますコンテンツ新しい場所情報など

Intent i = new Intent(); 
    i.setAction(CUSTOM_INTENT); 
    context.sendBroadcast(i); 

カスタムを見るexampleを参照してください。

0

ActivityをバインドしてServiceに「購読する」必要があります。 Serviceは、Activityから受け取ったIntentのデータを介してタイプMessengerのオブジェクトを受け取ることができます。 Activityの中に単純なHandlerとそれを使用するMessengerを定義することができます。このようにしてServiceMessageのオブジェクトをActivityに送信し、Messageの内部に必要なすべての情報を送信できます。

関連する問題