Last active
August 29, 2015 14:04
-
-
Save gabrielemariotti/9ca266e4966a9722a856 to your computer and use it in GitHub Desktop.
Android Wear: small gist to start an Activity on the mobile handheld from the Android Wear device using the Teleport library. (https://github.com/Mariuxtheone/Teleport)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<service android:name=".ListenerServiceFromWear"> | |
<intent-filter> | |
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> | |
</intent-filter> | |
</service> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
wearApp project(':wear') | |
compile 'com.google.android.gms:play-services-wearable:+' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListenerServiceFromWear extends WearableListenerService { | |
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear"; | |
@Override | |
public void onMessageReceived(MessageEvent messageEvent) { | |
/* | |
* Receive the message from wear | |
*/ | |
if (messageEvent.getPath().equals(HELLO_WORLD_WEAR_PATH)) { | |
Intent startIntent = new Intent(this, MyActivity.class); | |
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(startIntent); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile "com.google.android.support:wearable:1.0.+" | |
compile 'com.google.android.gms:play-services-wearable:+' | |
compile 'Teleport:teleportlib:0.1.1' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LaunchTeleportActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear"; | |
TeleportClient mTeleportClient; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_launch); | |
//Connect the GoogleApiClient | |
mTeleportClient = new TeleportClient(this); | |
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); | |
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { | |
@Override | |
public void onLayoutInflated(WatchViewStub stub) { | |
CircledImageView mCircledImageView = (CircledImageView) stub.findViewById(R.id.circle); | |
mCircledImageView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
sendMessage(); | |
} | |
}); | |
} | |
}); | |
} | |
/** | |
* Send message to mobile handheld | |
*/ | |
private void sendMessage() { | |
if (mTeleportClient.getGoogleApiClient()!=null && mTeleportClient.getGoogleApiClient().isConnected()) { | |
mTeleportClient.sendMessage(HELLO_WORLD_WEAR_PATH,null); | |
//This feature is not yet available with the 0.1.1 | |
//It allows to use a custom ResultCallback | |
/* | |
ResultCallback<MessageApi.SendMessageResult> callback = new ResultCallback<MessageApi.SendMessageResult>() { | |
@Override | |
public void onResult(MessageApi.SendMessageResult sendMessageResult) { | |
if (!sendMessageResult.getStatus().isSuccess()) { | |
Log.e("TAG", "Failed to send message with status code: " | |
+ sendMessageResult.getStatus().getStatusCode()); | |
} | |
} | |
}; | |
mTeleportClient.sendMessage(HELLO_WORLD_WEAR_PATH,null,callback); | |
*/ | |
}else{ | |
//Improve your code | |
} | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
mTeleportClient.connect(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
mTeleportClient.disconnect(); | |
} | |
@Override | |
public void onConnected(Bundle bundle) {} | |
@Override | |
public void onConnectionSuspended(int i) {} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
//Improve your code | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment