Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save balachandarlinks/ba5a01aa53002eee13a7f04421af478d to your computer and use it in GitHub Desktop.
Save balachandarlinks/ba5a01aa53002eee13a7f04421af478d to your computer and use it in GitHub Desktop.
Android Wearable Message API example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="schwiz.net.weartest" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "schwiz.net.weartest"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.google.android.gms:play-services-wearable:+'
compile project(":shared")
compile 'com.android.support:support-v4:20.0.+'
}
package schwiz.net.weartest;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Toast;
import android.content.Intent;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
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 net.schwiz.weartest.common.Constants;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
GoogleApiClient mGoogleApiClient;
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDetach() {
super.onDetach();
mGoogleApiClient.disconnect();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
rootView.findViewById(R.id.wearable).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mGoogleApiClient.isConnected()) {
new Thread(new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for(Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), Constants.PATH_NOTIFICAITON_MESSAGE, "Hello World".getBytes()).await();
if(!result.getStatus().isSuccess()){
Log.e("test", "error");
} else {
Log.i("test", "success!! sent to: " + node.getDisplayName());
}
}
}
}).start();
} else {
Log.e("test", "not connected");
}
}
});
rootView.findViewById(R.id.oldschool).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager man = (NotificationManager)getActivity().getSystemService(NOTIFICATION_SERVICE);
man.notify(0, getNotification().build());
}
});
return rootView;
}
NotificationCompat.Builder getNotification() {
Intent viewIntent = new Intent(getActivity(), Main.class);
// viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent = PendingIntent.getActivity(getActivity(), 0, viewIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity());
builder.setLargeIcon(BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_launcher));
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setSubText("Its Bed Time");
builder.setContentTitle("Wear Notification Test");
builder.setContentText("Wear Notification Text");
// builder.setContentIntent(viewPendingIntent);
return builder;
}
@Override
public void onConnected(Bundle bundle) {
Log.d("test", "onConnected");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e("test", "Failed to connect to Google API Client");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="schwiz.net.weartest" >
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name=".ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "schwiz.net.weartest"
minSdkVersion 20
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:+'
compile project(":shared")
compile 'com.google.android.gms:play-services-wearable:+'
}
package schwiz.net.weartest;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;
import net.schwiz.weartest.common.Constants;
/**
* Created by nschwermann on 6/28/14.
*/
public class ListenerService extends WearableListenerService{
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.i("test", "onMessageReceived()");
if(messageEvent.getPath().equals(Constants.PATH_NOTIFICAITON_MESSAGE)) {
final String message = new String(messageEvent.getData());
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setContentText(message);
b.setSmallIcon(R.drawable.ic_launcher);
b.setContentTitle("Test Notification");
b.setLocalOnly(true);
NotificationManagerCompat man = NotificationManagerCompat.from(this);
man.notify(0, b.build());
} else {
super.onMessageReceived(messageEvent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment