2014-10-21 9 views
6

WearableListenerService को जब मैं एक Activity और Service के बीच संचार के बारे में पढ़ा, मैंने पाया कि हम उपयोग कर सकते हैं या तोएक गतिविधि से डेटा भेजा जा रहा

  • IBinder
  • मैसेंजर
  • AIDL

मैं पहले दो में दिलचस्पी है। इसलिए जब मैंने इसे Activity और WearableListenerService के बीच संवाद करने के लिए कार्यान्वित करने का प्रयास किया, तो मुझे onBind फ़ंक्शन को ओवरराइड करने की आवश्यकता थी।

लेकिन फिर भी, मैं कह रहा यह

अंतिम विधि ओवरराइड नहीं कर सकते "onBind"

मैं जब मैं एक सामान्य Service का उपयोग इस तरह के एक त्रुटि मिलती है न एक संकलक त्रुटि हो रही है। तो,

1. करता है मतलब है कि हम एक Activity से WearableListenerService के साथ संवाद करने का उपयोग नहीं कर IBinder या Messenger दृष्टिकोण?

2. यदि हां, तो एक Activity से WearableListenerService को संदेश पारित करने के लिए (या एक गतिविधि से उस सेवा के एक सार्वजनिक विधि कॉल) अगले सबसे अच्छा तरीका क्या है?

+0

https://developer.android.com/training/wearables/data-layer/events।एचटीएमएल – pskink

+0

@pskink मैं एक गतिविधि से 'WearableListenerService' (या गतिविधि से 'पहनने योग्य लिस्टर सेवा' में सार्वजनिक फ़ंक्शन को ट्रिगर करने के बारे में बात कर रहा हूं)। लेकिन लिंक में डेटा लेयर एपीआई उदाहरण 'पहनने योग्य लिस्टर सेवा' की घटनाओं को सुनने का विवरण प्रदान करता है और अन्यथा – BlackCursor

+1

इसके साथ कभी नहीं खेला जाता है, लेकिन इसके लिए 'मैसेज रिसीव() 'पर नहीं है? – pskink

उत्तर

14

कुछ खोदने के बाद, मुझे समाधान मिला। उम्मीद है कि यह किसी और की मदद करता है।

हम फ़ंक्शंस का उपयोग करके WearableListenerService पर गतिविधि से संदेश भेज सकते हैं। Activity और WearableListenerService ही नोड (डिवाइस) पर कर रहे हैं, हम की जरूरत है नीचे के रूप में संदेश भेजने के लिए स्थानीय नोड (वर्तमान नोड जहाँ से संदेश भेजे जाने के) के कहने मिल

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

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

जिसका उपयोग फ़ोन से जुड़े अन्य उपकरणों (जैसे पहनने) की सूची प्राप्त करने के लिए किया जाता है।

तो, मैं सफलतापूर्वक WearableListenerService करने के लिए अपने गतिविधि से एक संदेश भेजने में सक्षम

गतिविधि कोड

public class PhoneActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ 

    private static final String TAG = "PhoneActivity"; 

    public static final String CONFIG_START = "config/start"; 
    public static final String CONFIG_STOP= "config/stop" 

    Intent intent; 
    TextView txtview; 

    GoogleApiClient mGoogleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_phone); 

     if(null == mGoogleApiClient) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(Wearable.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 
      Log.v(TAG, "GoogleApiClient created"); 
     } 

     if(!mGoogleApiClient.isConnected()){ 
      mGoogleApiClient.connect(); 
      Log.v(TAG, "Connecting to GoogleApiClient.."); 
     } 

     startService(new Intent(this, PhoneService.class)); 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     Log.v(TAG,"onConnectionSuspended called"); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.v(TAG,"onConnectionFailed called"); 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     Log.v(TAG,"onConnected called"); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.v(TAG, "onStart called"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.phone, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_start_) { 
      new SendActivityPhoneMessage(CONFIG_START,"").start(); 
     }else if (id == R.id.action__stop) { 
      new SendActivityPhoneMessage(CONFIG_STOP,"").start(); 
     }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 = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await(); 
      Node node = nodes.getNode(); 
      Log.v(TAG, "Activity Node is : "+node.getId()+ " - " + node.getDisplayName()); 
      MessageApi.SendMessageResult result = Wearable.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"); 
      } 
     } 
    } 
} 

सेवा संहिता

इस प्रकार था
संबंधित मुद्दे