2011-06-18 6 views
2

約1年前に@mnishから質問されたこのquestionに関連する質問があります。Androidローカルサービスのサンプル、bindservice()、ServiceConnection()

彼の質問とコードをご覧ください。彼はServiceConnection()を実装し、それをbindService()に渡します。これは、上部近くのServiceドキュメントのローカルサービスサンプルに従います。

ローカルサービスサンプルを実装したいので、@mnish question/answerの詳細を追加しようとしています。 ServiceConnectionで()@mnishは私を混乱させるこのラインを持っています

mService = ILocService.Stub.asInterface(iservice); 

私は@mnishこのコードを書いたが、誰もがILocServiceが何であるか任意のアイデアと私は自分ILocServiceの作成に取り掛かるかもしれない方法任意のアイデアを持っていない理解できますか?この構造体はどこに文書化されていますか? IBinderサービスの価値はどこから来ていますか?

答えて

4

彼はおそらくAndroidのインタフェース定義言語(AIDL) http://developer.android.com/guide/components/aidl.html

を使用しているので、彼は文書化のように、サーバ側の実装のスタブを使用しています

// This is called when the connection with the service has been 
// established, giving us the service object we can use to 
// interact with the service. We are communicating with our 
// service through an IDL interface, so get a client-side 
// representation of that from the raw service object. 
mService = IRemoteService.Stub.asInterface(service); 

をiservice参照がから来ていますアクティビティにサービスをバインドした後に呼び出されるonServiceConnectedメソッド。 bindServiceの呼び出しは、onServiceConnectedメソッドを実装するServiceConnectionに渡されます。

サービスの実装がローカルの場合は、「IRemoteService.Stub.asInterface(サービス)」は必要ありません。サービスをローカルサービスにキャストするだけで済みます。

ローカルサービスのサンプルは、サービスでこれを行います。

public class LocalService extends Service { 
    private NotificationManager mNM; 

    // Unique Identification Number for the Notification. 
    // We use it on Notification start, and to cancel it. 
    private int NOTIFICATION = R.string.local_service_started; 

    /** 
    * Class for clients to access. Because we know this service always 
    * runs in the same process as its clients, we don't need to deal with 
    * IPC. 
    */ 
    public class LocalBinder extends Binder { 
     LocalService getService() { 
     return LocalService.this; 
     } 
    } 

    ... 

} 

そして、これはServiceConnectionクラスでの活動に:ここ

private ServiceConnection mConnection = new ServiceConnection() { 
public void onServiceConnected(ComponentName className, IBinder service) { 
     // This is called when the connection with the service has been 
     // established, giving us the service object we can use to 
     // interact with the service. Because we have bound to a explicit 
     // service that we know is running in our own process, we can 
     // cast its IBinder to a concrete class and directly access it. 
     mBoundService = ((LocalService.LocalBinder)service).getService(); 

     // Tell the user about this for our demo. 
     Toast.makeText(Binding.this, R.string.local_service_connected, 
       Toast.LENGTH_SHORT).show(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     // This is called when the connection with the service has been 
     // unexpectedly disconnected -- that is, its process crashed. 
     // Because it is running in our same process, we should never 
     // see this happen. 
     mBoundService = null; 
     Toast.makeText(Binding.this, R.string.local_service_disconnected, 
      Toast.LENGTH_SHORT).show(); 
    } 
}; 
+0

ありがとう、私はそれを撃つだろう。しかし、上記のようにServiceService()とIBinderをbindService()に渡す[Local Service Sample](http://developer.android.com/reference/android/app/Service.html#LocalServiceSample)があります。私はそれがAIDLを使用しているとは思わない。多分@mnishがAIDLを使用していて、あなたが言うように、スタブの理由です。 – Marie

+0

サンプルが魔法を行う部分を追加しました – Tosa

+0

ごめんなさいわかりません。あなたは何を追加しましたか?ローカルサービスサンプルのLocalBinder?いいえ、すでにそこにいる。 mBoundService?いいえ、それはすでにです。すごく鈍いことに申し訳ありませんが、私はあなたが「サンプルがマジックを行う部分を追加しました」とは理解していません。あなたはあなたの答えを編集して、私はそれを見ていないだけですか? – Marie

1

あなたは、私の例..あなたが明確になります約LOL

// My MyServiceInterface.aidl 
package com.mad.exam; 

interface MyServiceInterface { 
    int getNumber(); 
} 

//MyService 
public class MyService extends Service { 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show(); 
     return mBinder; 
    } 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "Service Destroyed ", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 
     Toast.makeText(this, "Service Started ", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public boolean onUnbind(Intent intent) { 
     // TODO Auto-generated method stub 
     return super.onUnbind(intent); 
    } 

    private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() { 
     public int getNumber() { 
      return new Random().nextInt(100); 
     } 
    }; 
} 

//My Activity 
public class ServiceDemo extends Activity implements OnClickListener { 
    MyServiceInterface mService; 
    ServiceConnection mConnection; 
    Button retreive; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.service); 
     retreive = (Button) findViewById(R.id.retreive); 
     retreive.setOnClickListener(this); 

     mConnection = new ServiceConnection() { 

      public void onServiceDisconnected(ComponentName name) { 
       // TODO Auto-generated method stub 
      } 

      public void onServiceConnected(ComponentName name, IBinder service) { 
       // TODO Auto-generated method stub 
       mService = MyServiceInterface.Stub.asInterface(service); 

       try { 
        int i; 
        i = mService.getNumber(); 
        Toast.makeText(ServiceDemo.this, "The service value is: " + String.valueOf(i), Toast.LENGTH_SHORT).show(); 
       } catch (RemoteException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }; 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Log.i("My Tag", "Clicked"); 
     Button btn = (Button) v; 
     Intent callService = new Intent(this, MyService.class); 
     bindService(callService, mConnection, Context.BIND_AUTO_CREATE); 
    } 

    @Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
     Intent callService = new Intent(this, MyService.class); 
     bindService(callService, mConnection, Context.BIND_AUTO_CREATE); 
    } 
} 
+0

サービスが実行されておらず、起動していない場合はどうなりますか?これらの小切手はどこにありますか? – TheRealChx101

関連する問題