我從佩戴手錶發送數據字節。我想在手機的後臺服務中接收數據,但不會調用onMesageReceived。任何想法我做錯了什麼?我的第一次嘗試是在手機上運行的應用程序,但並不實用。在移動使用Google Message API接收服務中的數據
服務:
public class LampControlService extends Service implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {
private static final String TAG = "test";
private static final String WEAR_MESSAGE_PATH = "/message";
private GoogleApiClient mGoogleApiClient;
private ArrayAdapter<String> mAdapter;
@Override
public void onCreate(){
HandlerThread thread = new HandlerThread("test");
thread.start();
//---Build a new Google API client--
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.build();
if (mGoogleApiClient != null && !(mGoogleApiClient.isConnected() ||
mGoogleApiClient.isConnecting()))
mGoogleApiClient.connect();
}
public int onStartCommand(Intent intent, int flags, int startId){
return START_STICKY;
}
@Override
public void onDestroy() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnected(Bundle bundle) {
Wearable.MessageApi.addListener(mGoogleApiClient, this);
Log.d(TAG, "onConnected()");
}
@Override
public void onMessageReceived(final MessageEvent messageEvent) {
Log.d(TAG, "onMessageReceived()");
if (messageEvent.getPath().equalsIgnoreCase(WEAR_MESSAGE_PATH)) {
String lamp = new String(messageEvent.getData());
sendLampCommand(lamp);
}
else {
// super.onMessageReceived(messageEvent);
}
}
// @Override
public void onConnectionSuspended(int i) {
}
private static void sendLampCommand(String lamp){
// Send lamp command to web server
}
}
線在AndroidManifest補充說:
<service
android:name=".LampControlService">
<service android:name=".DataLayerListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</service>
好建議! – Henrik