Back to project page Android.Wear.Message.
The source code is released under:
MIT License
If you think the Android project Android.Wear.Message listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.iamnbty.androidwear; // w w w. java 2 s . com import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.wearable.MessageApi; import com.google.android.gms.wearable.Wearable; import com.iamnbty.androidwear.helper.ConnectedDeviceFinder; import com.iamnbty.androidwear.helper.GoogleApiHelper; import com.iamnbty.androidwear.model.ConnectedDevice; import java.io.ByteArrayOutputStream; import java.util.ArrayList; public class MainActivity extends ActionBarActivity implements GoogleApiHelper.Callback, View.OnClickListener { private static final int VIEW_POSITION_NO_CONNECTED_DEVICE = 0; private static final int VIEW_POSITION_SEND_MESSAGE = 1; private GoogleApiHelper mGoogleApiHelper; private ConnectedDeviceFinder mConnectedDeviceFinder; private ViewFlipper mViewFlipper; private TextView mConnectedDeviceTextView; private EditText mMessageEditText; private Button mSendButton; private ConnectedDevice mConnectedDevice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set content view setContentView(R.layout.activity_main); // view matching mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); mConnectedDeviceTextView = (TextView) findViewById(R.id.connected_device_textview); mMessageEditText = (EditText) findViewById(R.id.message_edittext); mSendButton = (Button) findViewById(R.id.send_button); // register View on click listener mSendButton.setOnClickListener(this); // setup Google Api helper mGoogleApiHelper = new GoogleApiHelper(this, this, Wearable.API); } @Override protected void onStart() { super.onStart(); mGoogleApiHelper.activate(); } @Override protected void onStop() { mGoogleApiHelper.deactivate(); super.onStop(); } @Override public void onGoogleApiClientReady() { if (mConnectedDeviceFinder == null) { mConnectedDeviceFinder = new ConnectedDeviceFinder(mGoogleApiHelper.getGoogleApiClient(), new ConnectedDeviceFinder.Callback() { @Override public void onDeviceFinderStarted(ConnectedDeviceFinder finder) { mViewFlipper.setDisplayedChild(VIEW_POSITION_NO_CONNECTED_DEVICE); } @Override public void onDeviceFinderFinished(ConnectedDeviceFinder finder, ArrayList<ConnectedDevice> connectedDevices) { if (connectedDevices.size() == 0) { // display no connected device layout mViewFlipper.setDisplayedChild(VIEW_POSITION_NO_CONNECTED_DEVICE); } else { // store connected device mConnectedDevice = connectedDevices.get(0); // set text mConnectedDeviceTextView.setText(mConnectedDevice.displayName); // display send message layout mViewFlipper.setDisplayedChild(VIEW_POSITION_SEND_MESSAGE); } } }); mConnectedDeviceFinder.execute(); } } @Override public void onGoogleApiClientNotReady() { } @Override public void onClick(View view) { if (view == mSendButton) { String message = mMessageEditText.getText().toString(); if (message.trim().length() == 0) { Toast.makeText(this, "Please enter your message.", Toast.LENGTH_SHORT).show(); InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(mMessageEditText, InputMethodManager.SHOW_FORCED); } else { sendMessage(message.trim(), R.drawable.ic_developer); } } } private void sendMessage(String message, int background) { String node = mConnectedDevice.nodeId; String path = message; byte[] data = toByteArray(BitmapFactory.decodeResource(getResources(), background)); Wearable.MessageApi.sendMessage( mGoogleApiHelper.getGoogleApiClient(), node, path, data).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { Toast.makeText(MainActivity.this, "Message has been sent.", Toast.LENGTH_SHORT).show(); } }); } private static byte[] toByteArray(Bitmap bitmap) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream); return byteStream.toByteArray(); } }