在Android Wear上未调用onDataChanged [英] onDataChanged isn´t called on android wear

查看:83
本文介绍了在Android Wear上未调用onDataChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从智能手机向Android Wear发送简单的字符串。我之前看过以下问题:无法将数据推送到Android Wear(仿真器) )
并尝试以相同的方式进行操作,但是onDataChanged仅在我删除手机上的小孔App时有效,而在调用pushStringstoWear时不起作用。
希望获得帮助...

I want to send just simple Strings from Smartphone to android wear. I looked in this Question before: Unable to push data to android wear (Emulator) and tried to do the same way, but onDataChanged only when i delete the hole App on my Phone, but not when push StringstoWear is called. Hope for Help...

这是我的电话代码:
软件包de.gruppe8.culturehelper;

Here is my Code on Phone: package de.gruppe8.culturehelper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.Wearable;

import java.util.Date;

public class Phone extends AppCompatActivity implements          GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener     {

private GoogleApiClient mGoogleApiClient;
private static final String TAG = "PHONE";

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

    //Erstelle Google API Client zur Kommunikation mit Wear
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    //Testbutton zum Daten versenden
    Button button = (Button) findViewById(R.id.Testbutton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pushStringsToWear();
        }
    });

}

//Sendet Daten an Wear, wenn aufgerufen  !!!!--->Auchtung wird noch nicht         richtig empfangen
private void pushStringsToWear() {

    Inhalt.TEXT ="ätzend";
    //Requester
    PutDataMapRequest Sender =     PutDataMapRequest.create(Inhalt.NOTIFICATION_PATH);

       //Diese Daten werden versendet, unter dem angegeben Pfad (siehe hier drüber)
    Sender.getDataMap().putLong("time", System.currentTimeMillis());
    Sender.getDataMap().putString("Bilddateiname", Inhalt.IMAGE);
    Sender.getDataMap().putString("Text", Inhalt.TEXT);


    Wearable.DataApi.putDataItem(mGoogleApiClient,     Sender.asPutDataRequest());

    //bestätigt das aufrufen der Funktion
    TextView Information = (TextView) findViewById(R.id.Information);
    String a= "dgdfjkjhg";
    Information.setText(a);
}

//zu versendende Daten--->müssen anhand der Datenbank vor dem Absenden     verändert werden
public static class Inhalt {

    public static  String NOTIFICATION_PATH = "/Warning";
    public static  Long Zeit = System.currentTimeMillis();
    public static  String IMAGE = "Image";
    public static String TEXT = "content";


}

//Pflichtfunktionen onStart,onStop,onConnected,onConnectionFailed/Suspended,     onStop
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    Log.d(TAG, "connected to APICLIENT" + new Date().getTime());
}
@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "Connected");
}
@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "Connection Suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "Connection Failed");
}


@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
    Log.d(TAG, "disconected from APICLIENT" + new Date().getTime());
}



}

这是我在Wear上的代码:

And this is my code on Wear:

package de.gruppe8.culturehelper;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Wearable;

import java.util.Objects;

public class Watch extends Activity implements      GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,   DataApi.DataListener, MessageApi.MessageListener {

private GoogleApiClient mGoogleApiClient;
private static final String TAG = "WEAR";

private TextView mTextView;
GoogleApiAvailability a;

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

    //Starten des Google API Clients zur kommunikation mit Smartphone

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

//Wenn Daten vom Handy abgesendet werden, werden sie hier empfangen ---> Derzeit nur bei Deinstallation der App
public void onDataChanged(DataEventBuffer dataEvent) {
    for (DataEvent event : dataEvent) {
        if (event.getType() == DataEvent.TYPE_CHANGED &&
                event.getDataItem().getUri().getPath().equals("/Warning")) {


            final DataMapItem dMI = DataMapItem.fromDataItem(event.getDataItem());
            //Empfangbare Daten, zur weiterverarbeitung If states einbauen und Folge festlegen(externe Funktionen?)
            String Text = dMI.getDataMap().getString("Text");
            String Bild = dMI.getDataMap().getString("Bilddateiname");
            Long Zeit = dMI.getDataMap().getLong("time");

            //Teststring
                TextView Testergebnis = (TextView) findViewById(R.id.text);
            String s= Objects.toString(Zeit, null);
                Testergebnis.setText(s);
        }
    }
}


@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
//Startet die Listener
@Override
public void onConnected(Bundle bundle) {
    Wearable.DataApi.addListener(mGoogleApiClient, this);
    Wearable.MessageApi.addListener(mGoogleApiClient, this);
}

@Override
protected void onStop() {
    if (null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
        Wearable.DataApi.removeListener(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}



@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "Connection Suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "Connection Failed");
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {

}
}

穿戴清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.gruppe8.culturehelper">

<uses-feature android:name="android.hardware.type.watch" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <activity
        android:name=".Watch"
        android:label="@string/app_name"
        android:exported="true"
        android:allowEmbedded="true"
        android:taskAffinity=""
        android:theme="@android:style/Theme.DeviceDefault.Light">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- ATTENTION: This was auto-generated to add Google Play services to     your project for
         App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more   information. -->
    <meta-data android:name="com.google.android.gms.version"    android:value="@integer/google_play_services_version"/>
</application>

电话清单:

Phone Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Phone">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- ATTENTION: This was auto-generated to add Google Play services to  your project for
         App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>

推荐答案

播放开始在Services 8.3 中,使用DataApi进行的数据同步已分批处理并以一定的频率完成,这意味着您的数据同步最多可延迟30分钟。如果您需要立即同步,则需要确保将请求设置为紧急。附带说明一下,在使用ApiClient时(例如在 pushStringsToWear

Starting with Play Services 8.3, data synchronization using DataApi is batched up and done with a certain frequency which means synchronization of your data can be delayed up to 30 minutes. If you need immediate sync, you need to make sure your request is set as urgent. As a side note, it is a good practice to check for the connectivity of the ApiClient when you are using it, for example in your pushStringsToWear

这篇关于在Android Wear上未调用onDataChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆