当 mGoogleApiClient.connect() 似乎没有实际连接时,如何通过它?

问题描述 投票:0回答:1

我正在尝试从移动设备发送 ArrayList 到我的 Android Wear 设备。

不幸的是,我没有按照我的 sop 声明通过 mGoogleApiClient.connect() 。

我所做的是设置一个 DataLayer 项,并使用 onDataChanged() 来监视代码和与之一起发送的 DataItem,但不幸的是,似乎什么都没有传达。

控制台输出位于帖子底部,如果这有助于查明任何内容。

主要活动 - 移动

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

Set<String> tasksSet;
ArrayList<String> actsList;
ArrayAdapter adapter;
ListView list;
GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    mGoogleApiClient.connect();

    if(!PreferenceManager.getDefaultSharedPreferences(this)
            .getStringSet("wristaroo", new HashSet<String>()).isEmpty()) {
        tasksSet = PreferenceManager.getDefaultSharedPreferences(this)
                .getStringSet("wristaroo", new HashSet<String>());
        actsList = new ArrayList<String>(tasksSet);
    }
    else {
        actsList = new ArrayList<>();
        Set<String> tasksSet = new HashSet<>(actsList);
        PreferenceManager.getDefaultSharedPreferences(this)
                .edit()
                .putStringSet("wristaroo", tasksSet)
                .commit();
    }

    xxxxxxx
    other populating code
    xxxxxxx

    Button btnSend = (Button) findViewById(R.id.sendButton);
    btnSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendCustomSchedule(actsList);
            System.out.println("Button Pressed: Send");
        }
    });
}

private void populateListView() {
    list = (ListView) findViewById(R.id.listViewAdd);
    list.setAdapter(adapter);
}

public MainActivity getActivity() {
    return this;
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

public void sendCustomSchedule(ArrayList<String> customSchedule) {
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/customSched");

    putDataMapRequest.getDataMap().putStringArrayList("customSched", customSchedule);

    PutDataRequest request = putDataMapRequest.asPutDataRequest().setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, request)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if(!dataItemResult.getStatus().isSuccess()) {
                        System.out.println("Apparent Failure!");
                    } else {

                        System.out.println("Apparent Success!");
                    }
                }
            });
    System.out.println("When in doubt...");
    }
}

主要活动 - 穿着

 public class byTimeActivity extends Activity  implements DataApi.DataListener,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener   {
ArrayList<String> choicesList;
ArrayAdapter adapter;
ListView list;
ArrayList<String> data;
String dayExtra;
String howExtra;
String schedExtra;
GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


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

    mGoogleApiClient.connect();

    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

    dayExtra = getIntent().getExtras().getString("dayExtra");
    schedExtra = getIntent().getExtras().getString("schedExtra");
    howExtra = getIntent().getExtras().getString("howExtra");

    System.out.println("dayExtra: " + dayExtra);
    System.out.println("schedExtra: " + schedExtra);
    System.out.println("howExtra: " + howExtra);

    switch(dayExtra) {
        case "Thursday":
            if(schedExtra.equals("Full Schedule")) {
                setStage(thTime);
            }
            if(schedExtra.equals("Custom Schedule")) {
                adapter = new ArrayAdapter<>(this, R.layout.da_item, data);
            }
            break;
        case "Friday":
            setStage(frTime);
            break;
        case "Saturday":
            setStage(saTime);
            break;
        case "Sunday":
            setStage(suTime);
            break;
    }
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            list = (ListView) findViewById(R.id.mainChoiceList);
            list.setAdapter(adapter);
        }
    });
}

private void setStage(String[] dayStage) {
    choicesList = new ArrayList<>(Arrays.asList(dayStage));
    adapter = new ArrayAdapter<>(this, R.layout.da_item, choicesList);
    adapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
    super.onResume();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Wearable.DataApi.addListener(mGoogleApiClient, this);
}

@Override
protected void onPause() {
    super.onPause();
    Wearable.DataApi.removeListener(mGoogleApiClient, this);
    mGoogleApiClient.disconnect();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataItem item = event.getDataItem();
            if (item.getUri().getPath().compareTo("/customSched") == 0) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                data = dataMap.getStringArrayList("customSched");

                System.out.println("Data: " + data);
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            //dataItem Deleted
        }
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

控制台输出 - 移动

W/System: ClassLoader referenced unknown path: /data/app/com.conedmiro.schedaroo-2/lib/arm
I/GMPM: App measurement is starting up, version: 8487
I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
E/GMPM: Scheduler not set. Not logging error/warn.
E/GMPM: Uploading is not possible. App measurement disabled
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/Adreno200-EGL: <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.059_msm8960_JB_2.5.5_CL3896081_release_AU (CL3896081)
        Build Date: 06/25/13 Tue
        Local Branch:
        Remote Branch: quic/jb_2.5.5
        Local Patches: NONE
        Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.059 +  NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
I/System.out: When in doubt...
I/System.out: Button Pressed: Send
I/System.out: Apparent Success!

控制台输出 - 磨损

W/art: Suspending all threads took: 17.121ms
I/art: Background sticky concurrent mark sweep GC freed 1601(90KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 508KB/663KB, paused 20.603ms total 53.990ms
W/art: Suspending all threads took: 5.855ms
I/art: Background partial concurrent mark sweep GC freed 168(48KB) AllocSpace objects, 0(0B) LOS objects, 45% free, 611KB/1123KB, paused 8.179ms total 61.166ms
I/System.out: mGoogleApiClient connected!
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8487000 but found 8299574
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
                                                                                       
      [ 04-03 23:16:13.428 15148:15148 D/         ]
      HostConnection::get() New Host Connection established 0xb3edca60, tid 15148
D/Atlas: Validating map...
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f625e0, error=EGL_SUCCESS
I/System.out: mGoogleApiClient disconnected! //dis/connecting in MainActivity, before switching activities
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f628a0, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f62700, error=EGL_SUCCESS
I/System.out: schedExtra: Custom Schedule
I/System.out: dayExtra: Thursday
I/System.out: howExtra: By Time
I/System.out: dayExtra: Thursday
I/System.out: schedExtra: Custom Schedule
I/System.out: howExtra: By Time
I/System.out: mGoogleApiClient connected!
D/AndroidRuntime: Shutting down VM                                                                                       
                                                                                       
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.conedmiro.schedaroo, PID: 15148
      java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8487000 but found 8299574
java arraylist android-wear-data-api dataitem
1个回答
0
投票

WearableListenerService 是必需的,无论如何

仅在文档中简要提及[绝对应该更新]

© www.soinside.com 2019 - 2024. All rights reserved.