2012-03-31 15 views
17

からサービスに渡すパラメータ私はこの方法でサービスに結合しています:アンドロイド:活動

Activityクラス:

ListenLocationService mService; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
     ... 
     Intent intent = new Intent(this, ListenLocationService.class); 
     intent.putExtra("From", "Main"); 
     bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
     ... 
} 

private ServiceConnection mConnection = new ServiceConnection() { 

     public void onServiceConnected(ComponentName className, 
       IBinder service) { 
      LocalBinder binder = (LocalBinder) service; 
      mService = binder.getService();   
     } 

     public void onServiceDisconnected(ComponentName arg0) { 
     } 
    }; 

これは私のサービスonBind方法であり、

@Override 
public IBinder onBind(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    if(extras == null) 
     Log.d("Service","null"); 
    else 
    { 
     Log.d("Service","not null"); 
     String from = (String) extras.get("From"); 
     if(from.equalsIgnoreCase("Main")) 
      StartListenLocation(); 
    } 
    return mBinder; 
} 

だから私はLogCatに "null"を持っています - バンドルはs私が作ったのは、intent.putExtraの前にbindService

一般的にサービスはうまくいきます。しかし、私はStartListenLocation();にアプリケーションのメインアクティビティからのみ呼び出す必要があります(私はフラグを送信することによりこれを行うことにしました)。

サービスにデータを送信するにはどうすればよいですか?または、起動したアクティビティを確認する別の方法がありますか?onBind

答えて

8

1あなたはActivityから呼び出したいすべてのメソッドのシグネチャを宣言インタフェースの作成:あなたのバインダーがILocaionServiceを実装して作成し、実際のメソッド本体定義

public interface ILocationService { 
    public void StartListenLocation(Location location); 
} 

2:

public class MyBinder extends Binder implements ILocationService { 
    ... ... 

    public void StartListenLocation(Location location) { 
    // implement your method properly 
    } 

    ... ... 
} 

3サービスにバインドするアクティビティでは、バインダをインタフェースで参照してください。

... ... 

ILocationService mService; // communication is handled via Binder not the actual service class. 

private ServiceConnection mConnection = new ServiceConnection() { 

    public void onServiceConnected(ComponentName className, IBinder service) { 
    mService = (ILocationService) service;  
    } 

    ... ... 
}; 

... ... 

// At some point if you need call service method with parameter: 
Location location = new Location(); 
mService.StartListenLocation(location); 

すべての通信(すなわち、サービスへのメソッド呼び出し)は、実際のサービスクラス(binder.getService()は不要)でなく、ServiceConnection.onServiceConnected()でバインドクラスinitializeとreturnで処理して実行する必要があります。これは、APIで動作するように設計されたバインドサービス通信です。

bindService()は非同期呼び出しであることに注意してください。 bindService()を呼び出した後、ServiceConnection.onServiceConnected()コールバックがシステムに関与する前に、遅延が発生します。したがって、サービスメソッドを実行する最も良い場所は、mServiceがServiceConnection.onServiceConnected()メソッドで初期化された直後です。

これが役に立ちます。

+0

感謝を楽しみ、あなたのサービスクラスのONSTART方法で

@Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Bundle extras = intent.getExtras(); if(extras == null) Log.d("Service","null"); else { Log.d("Service","not null"); String from = (String) extras.get("From"); if(from.equalsIgnoreCase("Main")) StartListenLocation(); } } 

Intent serviceIntent = new Intent(this,ListenLocationService.class); serviceIntent.putExtra("From", "Main"); startService(serviceIntent); 

とパラメータを取得します。私は後で別の問題に直面した、多分あなたはそれを解決する方法を知っている:http://stackoverflow.com/questions/9956601/android-set-and-get-services-variable-in-different-activities –

+0

@yorkw "これはどのようにAPIで動作するように設計されたサービス通信をバインドする」 - 正しいとは思っていませんが、これについてもっと読むことができる公式のAndroidドキュメントへのリンクがありますか? – DuneCat

+2

@DuneCat、私は声明が非常に正確ではないと同意した、実際には、Googleがバウンドサービスを実装するために提案した推奨方法の1つと言及されている。[here](http://developer.android.com/guide/components/bound -services.html#Binder)を参照してください。 – yorkw

0

http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

一つの方法サービスにデータを送信するこのセッションによれば、列のステータスを読み取ることができ、PID(プロセスID)、データ、ステータス、READYを含むデータベースを使用して結合し、ことによりますサービスがロードされているときには、呼び出し元のアクティビティで満たされたテーブルが読み込まれ、終了したら削除することができます。また、前の回答に示すようにAIDLが表示されます。

26

あなたはこの単純な方法でパラメータを渡すことができます - 答えを:)

+2

これは、要求通りにonBindメソッドにパラメータを渡す方法です。 @yorkwは、サービスからアクティビティを呼び出すための優れた一般的なソリューションを持っています。 –

関連する問題