Back to project page AndroidFortunes.
The source code is released under:
Apache License
If you think the Android project AndroidFortunes 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 org.codechimp.androidfortunes; /* www . j a v a 2s. c o m*/ import android.app.Notification; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.PendingResult; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.wearable.MessageApi; import com.google.android.gms.wearable.Node; import com.google.android.gms.wearable.NodeApi; import com.google.android.gms.wearable.Wearable; import java.util.Collection; import java.util.HashSet; import static android.support.v4.app.NotificationCompat.Builder; public class NotifyHelper { private static final String TAG = "NotifyHelper"; private static final String MSG_QUOTE = "/quote"; public static final int NOTIFICATION_ID = 10002; public static void notify(Context context, String quote) { SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(context); boolean prefWearOnly = sp.getBoolean( "prefWear", false); if (prefWearOnly) sendMessageToWear(context, quote); else showNotification(context, quote); } private static void showNotification(Context context, String quote) { NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle(); bigStyle.bigText(quote); Notification notificationBuilder = new Builder(context) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(context.getString(R.string.app_name)) .setContentText(quote) .setStyle(bigStyle) .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(NOTIFICATION_ID, notificationBuilder); } public static void sendMessageToWear(Context context, final String quote) { // Connect to wear final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context) .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle connectionHint) { Log.d(TAG, "onConnected: " + connectionHint); // Now you can use the data layer API } @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(); googleApiClient.blockingConnect(); NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( googleApiClient, node.getId(), MSG_QUOTE, quote.getBytes()).await(); if (!result.getStatus().isSuccess()) { Log.e(TAG, "ERROR: failed to send Message: " + result.getStatus()); } } googleApiClient.disconnect(); } }