1

在过去的两天里,我已经阅读了关于这个主题的任何主题。但我仍然无法弄清楚我的代码有什么问题。

我有移动和穿戴应用程序。手机只有小部件和小部件配置活动(没有主要活动)。

这是移动应用程序文件:

AndroidManifest:(简单的 appwidget 和 appwidget_config 活动)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.xx.yy.remotedoor">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/housek"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".DoorConf"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

        <receiver android:name=".upDoor">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/up_door_info" />
        </receiver>
    </application>

</manifest>

upDoor.java:(查看 send_to_wear 函数)

package net.xx.yy.remotedoor;
import ...
public class upDoor extends AppWidgetProvider {
    public static String TAG = "Remote door GAPI";
    public static String CLICK_DOOR = "ClickDoorOpen";
    public static String DOOR_ID = "DoorID";

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        CharSequence widgetText = "Test";
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.up_door_widget);
        views.setTextViewText(R.id.textView, widgetText);
        Intent intent = new Intent(context, upDoor.class);
        intent.setAction(CLICK_DOOR);
        intent.putExtra(DOOR_ID, Integer.toString(appWidgetId));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.houseIcon, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
        send_to_wear(context);
        }
    }

    private static void send_to_wear(Context context) {
        // Sending data to wear
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                        Log.d(TAG, "onConnected: " + connectionHint);
                    }
                    @Override
                    public void onConnectionSuspended(int cause) {
                        Log.d(TAG, "onConnectionSuspended: " + cause);
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                        Log.d(TAG, "onConnectionFailed: " + result);
                    }
                })
                .addApi(Wearable.API)
                .build();
        Log.w("Google API", "Connecting");
        mGoogleApiClient.connect();
        Log.w("Google API", Boolean.toString(mGoogleApiClient.isConnected()));
        PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/doors/1");
        putDataMapReq.getDataMap().putString("caption", "test");            putDataMapReq.getDataMap().putLong("Time",System.currentTimeMillis());
        putDataMapReq.setUrgent();
        PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
        com.google.android.gms.common.api.ResultCallback<DataApi.DataItemResult> send_callback =
                new ResultCallback<DataApi.DataItemResult>() {
                    @Override
                    public void onResult(@NonNull DataApi.DataItemResult result) {
                        if (!result.getStatus().isSuccess()) {
                            Log.w("Result", "Failed");
                        }else{
                            Log.w("Result", "Success");
                        }
                        Log.w("Item", result.getDataItem().getUri().toString());
                        Log.w("Reason", result.getStatus().getStatusMessage());
                    }
                };
        com.google.android.gms.common.api.PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
        pendingResult.setResultCallback(send_callback);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {...}

    @Override
    public void onDisabled(Context context) {...}

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equals(CLICK_DOOR)) {
            send_to_wear(context);
        }
    }
}

在磨损方面:清单:(我将其配置为相同的包名称,可以吗?)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.xx.yy.remotedoor">

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/housek"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <activity
            android:name=".main"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".dataLayer" android:exported="false" android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
                <data android:scheme="wear" android:host="*" android:pathPrefix="*" />
            </intent-filter>
        </service>

    </application>

</manifest>

和dataLayer java部分:

package net.xx.yy.remotedoor;
import ...
public class dataLayer extends WearableListenerService { //implements DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private static final String TAG = "LOG";
    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {

        Log.w(TAG, "Wear got on_change");
        final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);

        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();

        // Do I need it??
        ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
        if (!connectionResult.isSuccess()) {
            Log.e(TAG, "Failed to connect to GoogleApiClient.");
            return;
        }

        for (DataEvent dataEvent : events) {
            if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
                DataItem item = dataEvent.getDataItem();
                Log.w(TAG, "item path: " + item.getUri().getPath());
                if(item.getUri().getPath().startsWith("/doors/")){
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                    // Do something
                }
            }
        }
    }
}

在移动日志上我可以看到:

  1. 数据项结果:成功
  2. 项目:wear://HOST_ID/doors/1

在磨损日志上,我看不到与此相关的任何内容..

我究竟做错了什么!?

谢谢!!

4

0 回答 0