Android穿,从服务发送响应

时间:2015-07-01 15:04:54

标签: service request response wear-os

按下磨损应用程序上的按钮,它会向手机发送消息。它通过移动设备上运行的服务接收消息。但是当从移动设备发回响应消息到磨损时,似乎没有用。我正在使用(https://github.com/Mariuxtheone)的库。

佩戴主要活动

import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.wearable.DataMap;
import com.mariux.teleport.lib.TeleportClient;
import com.mariux.teleport.lib.TeleportService;

public class MainActivity extends Activity {

    private static final String LOG_TAG = "MainActivity";

    private TeleportClient mTeleportClient;

    @Override
    protected void onStart() {
        super.onStart();
        mTeleportClient.connect();
    }

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

        mTeleportClient = new TeleportClient(this);

        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTeleportClient.sendMessage("message from wearable", null);

                mTeleportClient.syncString("", "");
            }
        });

        mTeleportClient.setOnGetMessageTask(new TeleportClient.OnGetMessageTask() {
            @Override
            protected void onPostExecute(String path) {
                Toast.makeText(MainActivity.this, "setOnGetMessageTask#onPostExecute("+path+")", Toast.LENGTH_SHORT).show();
            }
        });
        mTeleportClient.setOnSyncDataItemTask(new TeleportClient.OnSyncDataItemTask() {
            @Override
            protected void onPostExecute(DataMap result) {
                Toast.makeText(MainActivity.this, "setOnSyncDataItemTask#onPostExecute("+result.toString()+")", Toast.LENGTH_SHORT).show();
            }
        });

        mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
        mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
    }

    @Override
    protected void onStop() {
        mTeleportClient.disconnect();
        super.onStop();
    }


//    public class GetMessageCallback extends TeleportClient.OnGetMessageCallback {
//
//        @Override
//        public void onCallback(String dataMap) {
//            switch (dataMap) {
//                case "hello": {
//                    Toast.makeText(getApplicationContext(), "GETSTRTING", Toast.LENGTH_SHORT).show();
//                    //mTeleportClient.syncString("hello", "Hello, World!");
//                    mTeleportClient.sendMessage("hello2", null);
//
//                }
//            }
//        }
//    }
//
//
//    public class ShowToastHelloWorldTask extends TeleportClient.OnSyncDataItemTask {
//
//        @Override
//        protected void onCancelled() {
//            super.onCancelled();
//            Log.e(LOG_TAG, "onCancelled()");
//        }
//
//        @Override
//        protected void onPostExecute(DataMap dataMap) {
//            Log.e(LOG_TAG, "onPostExecute()");
//
//            String hello = dataMap.getString("hello");
//            Toast.makeText(getApplicationContext(), hello, Toast.LENGTH_SHORT).show();
//
//
//            mTeleportClient.setOnSyncDataItemTask(new ShowToastHelloWorldTask());
//
//        }
//
//
//    }


    public class OnSyncDataItemCallback extends TeleportClient.OnSyncDataItemCallback {

        @Override
        public void onDataSync(DataMap dataMap) {
            Log.i(LOG_TAG, "onDataSync()");

            Toast.makeText(MainActivity.this, "DataMap dataMap", Toast.LENGTH_SHORT).show();
            mTeleportClient.sendMessage("message from wearable", null);

            mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
        }
    }

    public class OnGetMessageCallback extends TeleportClient.OnGetMessageCallback {

        @Override
        public void onCallback(String message) {
            Log.i(LOG_TAG, "onCallback()");

            Toast.makeText(MainActivity.this, "GETSTRTING", Toast.LENGTH_SHORT).show();



            mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());

        }
    }
}

移动主要活动

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.mariux.teleport.lib.TeleportClient;


public class MainActivity extends ActionBarActivity {

    private TeleportClient mTeleportClient;

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

        mTeleportClient = new TeleportClient(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mTeleportClient.connect();
    }

    @Override
    protected void onStop() {
        mTeleportClient.disconnect();
        super.onStop();
    }
}

移动服务

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.wearable.DataMap;
import com.mariux.teleport.lib.TeleportClient;
import com.mariux.teleport.lib.TeleportService;

/**
 * Created by aFrogleap on 7/1/2015.
 */
public class WearService extends TeleportService {

    private static final String LOG_TAG = "WearService";

    private TeleportClient mTeleportClient;

    @Override
    public void onStart(Intent intent, int startId) {
        Log.i(LOG_TAG, "onStart()");
        super.onStart(intent, startId);

        mTeleportClient.connect();
        mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
        mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());

        mTeleportClient.setOnGetMessageTask(new TeleportClient.OnGetMessageTask() {
            @Override
            protected void onPostExecute(String path) {
                Toast.makeText(WearService.this, "setOnGetMessageTask#onPostExecute(" + path + ")", Toast.LENGTH_SHORT).show();
            }
        });
        mTeleportClient.setOnSyncDataItemTask(new TeleportClient.OnSyncDataItemTask() {
            @Override
            protected void onPostExecute(DataMap result) {
                Toast.makeText(WearService.this, "setOnSyncDataItemTask#onPostExecute(" + result.toString() + ")", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onCreate() {
        Log.i(LOG_TAG, "onCreate()");

        super.onCreate();
        mTeleportClient = new TeleportClient(this);

        //setOnGetMessageCallback(new StartActivityTask());

    }

    @Override
    public void onDestroy() {
        Log.i(LOG_TAG, "onDestroy()");

        super.onDestroy();

        mTeleportClient.disconnect();
    }


    public class OnSyncDataItemCallback extends TeleportClient.OnSyncDataItemCallback {

        @Override
        public void onDataSync(DataMap dataMap) {
            Log.i(LOG_TAG, "onDataSync()");

            Toast.makeText(WearService.this, "DataMap dataMap", Toast.LENGTH_SHORT).show();
            mTeleportClient.sendMessage("message from device", null);

            mTeleportClient.setOnSyncDataItemCallback(new OnSyncDataItemCallback());
        }
    }

    public class OnGetMessageCallback extends TeleportClient.OnGetMessageCallback {

        @Override
        public void onCallback(String message) {
            Log.i(LOG_TAG, "onCallback()");

            switch (message) {
                case "hello": {
                    Toast.makeText(WearService.this, "GETSTRTING", Toast.LENGTH_SHORT).show();
                    mTeleportClient.sendMessage("message from device", null);

                }

                mTeleportClient.setOnGetMessageCallback(new OnGetMessageCallback());
            }
        }
    }
}

有人也对这个问题有所了解吗?真的需要一些帮助解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的移动服务不需要Teleport客户端,因为他是一个。 - >见TeleportService Doc

您的移动服务应该在onCreate期间setOnGetMessage。

您的移动服务应该填写数据地图而不是发回消息。