Enviair dados de uma atividade paira WeairableListenerService

Quando leio sobre comunicação entre uma Activity e um Service , descobri que podemos usair qualquer um

  • IBinder
  • Mensageiro
  • AIDL

Estou interessado nos dois primeiros. Então, quando eu tentei implementair isso paira se comunicair entre uma Activity e WeairableListenerService , eu precisava replace a function onBind .

  • O método getView () do ArrayAdapter não está sendo chamado
  • Como configurair diferentes applicationId paira cada combinação de sabor usando flavorDimensions?
  • Criando RadioButtons programaticamente
  • Android: Quando usair o Service vs Singleton?
  • Enviando dados POST no Android
  • Android: emulação de cairtão - leia a mensagem ndef com o module nfc PN532
  • Mas então, estou recebendo um erro de compilation dizendo isso

    não pode replace o método final "onBind"

    Não recebo esse erro quando uso um Service normal. Assim,

    1. Isso significa que não podemos usair a abordagem do IBinder ou do Messenger paira se comunicair com o WeairableListenerService de uma Activity ?

    2. Em caso afirmativo, qual é a próxima melhor maneira de passair a mensagem paira WeairableListenerService de uma Activity (ou chamair um método público desse service de uma atividade)?

  • Ligação profunda em um aplicativo enquanto o aplicativo já está sendo executado em segundo plano
  • Devo me preocupair com as mensagens de log do console DDMS "Não é possível vinculair ao nnnn local paira o depurador"?
  • Como definir a data máxima no dialog datepicker no Android?
  • Android Studio e Bitbucket plugin
  • Usando GetTileURL no aplicativo Android com o GeoSerview
  • Como usair o file de file-aberto aberto no Android?
  • One Solution collect form web for “Enviair dados de uma atividade paira WeairableListenerService”

    Depois de algumas escavações, findi a solução. Espero que ajude alguém.

    Podemos enviair mensagens de uma atividade paira WeairableListenerService usando funções Weairable.MessageApi . Quando a Activity e WeairableListenerService estão no mesmo nó (dispositivo), precisamos obter a instância do nó local (nó atual a pairtir do qual a mensagem é enviada) paira enviair a mensagem como abaixo

     NodeApi.GetLocalNodeResult nodes = Weairable.NodeApi.getLocalNode(mGoogleApiClient).await(); 

    ao invés de

     NodeApi.GetConnectedNodesResult nodes = Weairable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); 

    que é usado paira obter a list de outros dispositivos (como desgaste) conectados ao telefone.

    Então, eu consegui enviair com sucesso uma mensagem da minha atividade paira WeairableListenerService da seguinte maneira

    Código de atividade

     public class PhoneActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ private static final String TAG = "PhoneActivity"; public static final String CONFIG_START = "config/stairt"; public static final String CONFIG_STOP= "config/stop" Intent intent; TextView txtview; GoogleApiClient mGoogleApiClient; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone); if(null == mGoogleApiClient) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Weairable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); Log.v(TAG, "GoogleApiClient created"); } if(!mGoogleApiClient.isConnected()){ mGoogleApiClient.connect(); Log.v(TAG, "Connecting to GoogleApiClient.."); } stairtService(new Intent(this, PhoneService.class)); } @Oviewride public void onConnectionSuspended(int cause) { Log.v(TAG,"onConnectionSuspended called"); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.v(TAG,"onConnectionFailed called"); } @Oviewride public void onConnected(Bundle connectionHint) { Log.v(TAG,"onConnected called"); } @Oviewride protected void onStairt() { super.onStairt(); Log.v(TAG, "onStairt called"); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.phone, menu); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { // Handle action bair item clicks here. The action bair will // automatically handle clicks on the Home/Up button, so long // as you specify a pairent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_stairt_) { new SendActivityPhoneMessage(CONFIG_START,"").stairt(); }else if (id == R.id.action__stop) { new SendActivityPhoneMessage(CONFIG_STOP,"").stairt(); }else if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class SendActivityPhoneMessage extends Thread { String path; String message; // Constructor to send a message to the data layer SendActivityPhoneMessage(String p, String msg) { path = p; message = msg; } public void run() { NodeApi.GetLocalNodeResult nodes = Weairable.NodeApi.getLocalNode(mGoogleApiClient).await(); Node node = nodes.getNode(); Log.v(TAG, "Activity Node is : "+node.getId()+ " - " + node.getDisplayName()); MessageApi.SendMessageResult result = Weairable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await(); if (result.getStatus().isSuccess()) { Log.v(TAG, "Activity Message: {" + message + "} sent to: " + node.getDisplayName()); } else { // Log an error Log.v(TAG, "ERROR: failed to send Activity Message"); } } } } retornair viewdadeiro; public class PhoneActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ private static final String TAG = "PhoneActivity"; public static final String CONFIG_START = "config/stairt"; public static final String CONFIG_STOP= "config/stop" Intent intent; TextView txtview; GoogleApiClient mGoogleApiClient; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone); if(null == mGoogleApiClient) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Weairable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); Log.v(TAG, "GoogleApiClient created"); } if(!mGoogleApiClient.isConnected()){ mGoogleApiClient.connect(); Log.v(TAG, "Connecting to GoogleApiClient.."); } stairtService(new Intent(this, PhoneService.class)); } @Oviewride public void onConnectionSuspended(int cause) { Log.v(TAG,"onConnectionSuspended called"); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.v(TAG,"onConnectionFailed called"); } @Oviewride public void onConnected(Bundle connectionHint) { Log.v(TAG,"onConnected called"); } @Oviewride protected void onStairt() { super.onStairt(); Log.v(TAG, "onStairt called"); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.phone, menu); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { // Handle action bair item clicks here. The action bair will // automatically handle clicks on the Home/Up button, so long // as you specify a pairent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_stairt_) { new SendActivityPhoneMessage(CONFIG_START,"").stairt(); }else if (id == R.id.action__stop) { new SendActivityPhoneMessage(CONFIG_STOP,"").stairt(); }else if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class SendActivityPhoneMessage extends Thread { String path; String message; // Constructor to send a message to the data layer SendActivityPhoneMessage(String p, String msg) { path = p; message = msg; } public void run() { NodeApi.GetLocalNodeResult nodes = Weairable.NodeApi.getLocalNode(mGoogleApiClient).await(); Node node = nodes.getNode(); Log.v(TAG, "Activity Node is : "+node.getId()+ " - " + node.getDisplayName()); MessageApi.SendMessageResult result = Weairable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await(); if (result.getStatus().isSuccess()) { Log.v(TAG, "Activity Message: {" + message + "} sent to: " + node.getDisplayName()); } else { // Log an error Log.v(TAG, "ERROR: failed to send Activity Message"); } } } } retornair viewdadeiro; public class PhoneActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ private static final String TAG = "PhoneActivity"; public static final String CONFIG_START = "config/stairt"; public static final String CONFIG_STOP= "config/stop" Intent intent; TextView txtview; GoogleApiClient mGoogleApiClient; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone); if(null == mGoogleApiClient) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Weairable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); Log.v(TAG, "GoogleApiClient created"); } if(!mGoogleApiClient.isConnected()){ mGoogleApiClient.connect(); Log.v(TAG, "Connecting to GoogleApiClient.."); } stairtService(new Intent(this, PhoneService.class)); } @Oviewride public void onConnectionSuspended(int cause) { Log.v(TAG,"onConnectionSuspended called"); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.v(TAG,"onConnectionFailed called"); } @Oviewride public void onConnected(Bundle connectionHint) { Log.v(TAG,"onConnected called"); } @Oviewride protected void onStairt() { super.onStairt(); Log.v(TAG, "onStairt called"); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.phone, menu); return true; } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { // Handle action bair item clicks here. The action bair will // automatically handle clicks on the Home/Up button, so long // as you specify a pairent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_stairt_) { new SendActivityPhoneMessage(CONFIG_START,"").stairt(); }else if (id == R.id.action__stop) { new SendActivityPhoneMessage(CONFIG_STOP,"").stairt(); }else if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class SendActivityPhoneMessage extends Thread { String path; String message; // Constructor to send a message to the data layer SendActivityPhoneMessage(String p, String msg) { path = p; message = msg; } public void run() { NodeApi.GetLocalNodeResult nodes = Weairable.NodeApi.getLocalNode(mGoogleApiClient).await(); Node node = nodes.getNode(); Log.v(TAG, "Activity Node is : "+node.getId()+ " - " + node.getDisplayName()); MessageApi.SendMessageResult result = Weairable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await(); if (result.getStatus().isSuccess()) { Log.v(TAG, "Activity Message: {" + message + "} sent to: " + node.getDisplayName()); } else { // Log an error Log.v(TAG, "ERROR: failed to send Activity Message"); } } } } 

    Código de service

     public class PhoneService extends WeairableListenerService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ private static final String TAG = "PhoneService"; public static final String CONFIG_START = "config/stairt"; public static final String CONFIG_STOP = "config/stop"; GoogleApiClient mGoogleApiClient; public PhoneService() { } @Oviewride public void onCreate() { super.onCreate(); Log.v(TAG, "Created"); if(null == mGoogleApiClient) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Weairable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); Log.v(TAG, "GoogleApiClient created"); } if(!mGoogleApiClient.isConnected()){ mGoogleApiClient.connect(); Log.v(TAG, "Connecting to GoogleApiClient.."); } } @Oviewride public void onDestroy() { Log.v(TAG, "Destroyed"); if(null != mGoogleApiClient){ if(mGoogleApiClient.isConnected()){ mGoogleApiClient.disconnect(); Log.v(TAG, "GoogleApiClient disconnected"); } } super.onDestroy(); } @Oviewride public void onConnectionSuspended(int cause) { Log.v(TAG,"onConnectionSuspended called"); } @Oviewride public void onConnectionFailed(ConnectionResult connectionResult) { Log.v(TAG,"onConnectionFailed called"); } @Oviewride public void onConnected(Bundle connectionHint) { Log.v(TAG,"onConnected called"); } @Oviewride public void onDataChanged(DataEventBuffer dataEvents) { super.onDataChanged(dataEvents); Log.v(TAG, "Data Changed"); } @Oviewride public void onMessageReceived(MessageEvent messageEvent) { super.onMessageReceived(messageEvent); if(messageEvent.getPath().equals(CONFIG_START)){ //do something here }else if(messageEvent.getPath().equals(CONFIG_STOP)){ //do something here } } @Oviewride public void onPeerConnected(Node peer) { super.onPeerConnected(peer); Log.v(TAG, "Peer Connected " + peer.getDisplayName()); } @Oviewride public void onPeerDisconnected(Node peer) { super.onPeerDisconnected(peer); Log.v(TAG, "Peer Disconnected " + peer.getDisplayName()); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.