The Android Wear ecosystem seems to be built around quick tasks which a user will interact with, and then close. This works great for most applications, but what about one which covers a long running task, and should not be automatically closed when the watch sleeps?
My specific case: Swing by Swing Golf GPS. The preferred operation would be to have the application remain active, and shown when the screen wakes due to user action. And the life-time of a single use will be between 2 to 4 hours.
What are some methods to go about keeping an application front and center on the Android Wear device for periods longer than a single use?
So, here is what I have come up with as a solution:
Build a notification with a PendingIntent
to open the main Activity
. Also pass it an intent for the delete action, so we know if the user has dismissed it.
public class SbsNotificationHelper {
private static final String NOTIFICATION_DELETED_INTENT = "sbs.notificationDeleted";
private static boolean _isNotificationActive = false;
/** Public static methods */
public static NotificationCompat.Builder buildRoundInProgressNotification(Context context) throws Throwable {
Intent viewIntent = new Intent(context, SbsRoundActivity.class);
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
context.registerReceiver(_broadcastReceiver, new IntentFilter(NOTIFICATION_DELETED_INTENT));
_isNotificationActive = true;
Intent deleteIntent = new Intent(NOTIFICATION_DELETED_INTENT);
PendingIntent deletePendintIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.circle_button, "", viewPendingIntent).build();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bottom_bg);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.iphone_57x57)
.setLargeIcon(bitmap)
.setContentTitle("Golf GPS")
.setContentText("Swing by Swing")
.addAction(action)
.setDeleteIntent(deletePendintIntent)
.extend(new NotificationCompat.WearableExtender()
.setContentAction(0));
return notificationBuilder;
}
public static boolean isNotificationActive() {
return _isNotificationActive;
}
/** BroadcastReceiver */
private static final BroadcastReceiver _broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
_isNotificationActive = false;
}
};
}
Use onStop()
as opposed to onPause()
to issue the notification. This way, if you have multiple activities in your app, you can present them (only causing onPause()
of the main Activity
).
@Override
protected void onStop() {
super.onStop();
int notificationId = 001;
NotificationCompat.Builder notificationBuilder = SbsNotificationHelper.buildRoundInProgressNotification(context);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notificationId, notificationBuilder.build());
}
Also use the notification inside of your WearableListenerService
if you communicate with an app on the handheld. Thus a notification can be popped and easily accessed when your app is opened.
@Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
try {
if (SEND_MESSAGE_PATH.equalsIgnoreCase(messageEvent.getPath())) {
if (!SbsNotificationHelper.isNotificationActive()) {
int notificationId = 001;
NotificationCompat.Builder notificationBuilder = SbsNotificationHelper.buildRoundInProgressNotification(sbsApplication);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notificationId, notificationBuilder.build());
}
}
}
catch (Throwable throwable) {
//Handle errors
}
}
the OnPause()
method is called whenever the device is put to sleep or dialogue appears over the application. One or two activities can be done here, but they should be kept reasonably lightweight to prevent elongated user wait times.
I've had no problem doing a "extends Service" app on Wear device that works perfectly fine.
Basically: In your Wear app - decouple your GUI and app logic. Keep the app logic inside the service. I keep a class object that holds all the GUI data and pull it static from the service when Activity starts.
You could extend wearable service, but I use just the generic service as the center of my app and that worked perfectly fine (app runs for days without trouble).