WearableListenerService不是在app中创建的

时间:2016-12-09 00:49:01

标签: android android-service wear-os

我有一项服务来控制与应用程序和应用程序磨损的通信,但是,在此服务中,它不是创建的,因为在我调试时,它从不进入方法并且它没有与齿轮连接。

AndroidManifest

<service android:name=".ElUniversalWearService">
   <intent-filter>
   <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
   <data android:scheme="wear" android:host="*"
                  android:path="/start-activity" />
   <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />

 </intent-filter>

ElUniversalWearService

public class ElUniversalWearService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks,    GoogleApiClient.OnConnectionFailedListener{

  private GoogleApiClient googleApiClient=null;
  private Node connectedNode=null;

  @Override
  public void onCreate() {
    super.onCreate();
    googleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Wearable.API)
    .build();
    googleApiClient.connect();
  }

  @Override
  public void onMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);

    String message=messageEvent.getPath();

    if(message.equals(Operations.MESSAGE_INTERNET_VERIFY)){
      new InternetAsynkTask().execute();
    Log.d("[DEBUG-APP]", "Checa la conexión");
  }
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
  Log.e("[DEBUG-APP]", "Lee la información enviada desde el wear");
  for (DataEvent event : dataEvents) {
    String eventUri = event.getDataItem().getUri().toString();
    if (eventUri.contains(Operations.SECTION_REQUEST)) {
      DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
      String notesDataMaps = dataMapItem.getDataMap().getString(TagName.ARG_NAME_SECTION);
      String urlNoteDataMaps =    dataMapItem.getDataMap().getString(TagName.ARG_NAME_SECTION);
      new SendDataNoteTask(googleApiClient).execute(urlNoteDataMaps);
    }
  }
}

public void sendMessageToWear(String message){
  new SendMessageTask(googleApiClient).execute(message);
}

@Override
public void onConnected(@Nullable Bundle bundle) {
  Log.e("[DEBUG-APP]", "Se ha conectado al wear");
}

@Override
public void onConnectionSuspended(int i) {
  Log.e("[DEBUG-APP]", "Se ha suspendido el wear");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  Log.e("[DEBUG-APP]", "Se ha fallado al conectar al wear");
}

private class InternetAsynkTask extends AsyncTask<Void,Void,Boolean>{

@Override
protected Boolean doInBackground(Void... params) {
  if(CheckNetworkConnection.isConnectionAvailable(getBaseContext())){
    return true;
  }else{
    return false;
  }
}

@Override
protected void onPostExecute(Boolean aBoolean) {
    if(aBoolean)
      sendMessageToWear(Operations.MESSAGE_INTERNET_ENABLED);
    else
      sendMessageToWear(Operations.MESSAGE_INTERNET_DISABLED);
  }
 }
}

1 个答案:

答案 0 :(得分:0)

您可能希望通过Sending and Receiving Messages,其中注意到:

  

如果您创建扩展WearableListenerService以检测功能更改的服务,您可能希望覆盖onConnectedNodes()方法以收听更精细的连接详细信息。

有关如何收听重要事件的详细信息,请参阅Listen for Data Layer Events