Today’s post on #AndroidWear is from +Wayne Piekarski.
Now that the full Android Wear SDK is available, it’s time to port your existing wearable-enabled notification
code from the Developer Preview. In the process, you’ll switch to using
the latest Android support library, and there are some small API
changes that will require you to update your code. This article will
show you how to update my previous code samples that were released
earlier for stacks and pages, which you can use to guide the conversion of your own code as well.
code from the Developer Preview. In the process, you’ll switch to using
the latest Android support library, and there are some small API
changes that will require you to update your code. This article will
show you how to update my previous code samples that were released
earlier for stacks and pages, which you can use to guide the conversion of your own code as well.
To get started with an existing project in Android Studio, you should update to the 0.8 or later release.
You also need to make sure you’ve downloaded the Google Support Library
version 20 or later from the SDK Manager. Since this is only a
notification-based example, there’s no need to download the full Android
Wear SDK, which is only needed if you want to create an APK to run on
the wearable device.
You also need to make sure you’ve downloaded the Google Support Library
version 20 or later from the SDK Manager. Since this is only a
notification-based example, there’s no need to download the full Android
Wear SDK, which is only needed if you want to create an APK to run on
the wearable device.
Unix diff output is used to show the necessary changes in an easy to
understand way. Do not copy the + or - symbols at the start of each
line, and ignore the lines starting with @@ which are used to indicate
the line number that changed. For the curious, I used the following
command to generate the diff output from the last commit in my GIT
repository (the -U1 shows one line of context to keep the output
simple):
understand way. Do not copy the + or - symbols at the start of each
line, and ignore the lines starting with @@ which are used to indicate
the line number that changed. For the curious, I used the following
command to generate the diff output from the last commit in my GIT
repository (the -U1 shows one line of context to keep the output
simple):
git show HEAD -U1
Gradle changes
To add the new support-v4 library, you need to edit your build.gradle file like so:
@@ -24,2 +24,3 @@ dependencies {
compile 'com.android.support:appcompat-v7:19.+'
+ compile 'com.android.support:support-v4:20.0+'
}
Make sure you remove the wearable-preview-support.jar that was
provided with the Developer Preview from your libs directory and
build.gradle file, since these features are now in the standard support
library.
provided with the Developer Preview from your libs directory and
build.gradle file, since these features are now in the standard support
library.
Package imports
Since the APIs and package names have changed, the import statements
at the top of MainActivity.java need to be adjusted like this:
at the top of MainActivity.java need to be adjusted like this:
@@ -7,3 +7,2 @@ import android.view.MenuItem; -import android.support.v4.app.NotificationCompat; import android.app.Notification; @@ -13,4 +12,9 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.preview.support.v4.app.NotificationManagerCompat; -import android.preview.support.wearable.notifications.WearableNotifications; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; + +// Extra dependencies needed for the pages example +import java.util.ArrayList; +import java.util.List; +import android.support.v4.app.NotificationCompat.BigTextStyle;
Stacking notifications
Since the preview SDK, we have simplified how notifications are
implemented. The existing NotificationCompat.Builder() was extended to
support groups directly, instead of a separate WearableNotifications
class. The steps are a lot simpler, as can be seen with the following
changes to showStackNotifications():
implemented. The existing NotificationCompat.Builder() was extended to
support groups directly, instead of a separate WearableNotifications
class. The steps are a lot simpler, as can be seen with the following
changes to showStackNotifications():
@@ -63,3 +67,3 @@ public class MainActivity extends ActionBarActivity { // Group notification that will be visible on the phone - NotificationCompat.Builder builderG = new NotificationCompat.Builder(this) + Notification summaryNotification = new NotificationCompat.Builder(this) .setContentTitle("2 Pet Notifications") @@ -67,5 +71,5 @@ public class MainActivity extends ActionBarActivity { .setSmallIcon(R.drawable.ic_launcher) - .setLargeIcon(bitmapMila); - Notification summaryNotification = new WearableNotifications.Builder(builderG) - .setGroup(GROUP_KEY_MESSAGES, WearableNotifications.GROUP_ORDER_SUMMARY) + .setLargeIcon(bitmapMila) + .setGroup(GROUP_KEY_MESSAGES) + .setGroupSummary(true) .build(); @@ -76,3 +80,3 @@ public class MainActivity extends ActionBarActivity { PendingIntent.getActivity(this, notificationId+1, viewIntent1, 0); - NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this) + Notification notification1 = new NotificationCompat.Builder(this) .addAction(R.drawable.ic_action_done, "Treat Fed", viewPendingIntent1) @@ -81,4 +85,3 @@ public class MainActivity extends ActionBarActivity { + "Can we have steak?") - .setSmallIcon(R.drawable.ic_launcher); - Notification notification1 = new WearableNotifications.Builder(builder1) + .setSmallIcon(R.drawable.ic_launcher) .setGroup(GROUP_KEY_MESSAGES) @@ -89,3 +92,3 @@ public class MainActivity extends ActionBarActivity { PendingIntent.getActivity(this, notificationId+2, viewIntent2, 0); - NotificationCompat.Builder builder2 = new NotificationCompat.Builder(this) + Notification notification2 = new NotificationCompat.Builder(this) .addAction(R.drawable.ic_action_done, "Water Filled", viewPendingIntent2) @@ -93,4 +96,3 @@ public class MainActivity extends ActionBarActivity { .setContentText("Can you refill our water bowl?") - .setSmallIcon(R.drawable.ic_launcher); - Notification notification2 = new WearableNotifications.Builder(builder2) + .setSmallIcon(R.drawable.ic_launcher) .setGroup(GROUP_KEY_MESSAGES)
Page notifications
Page notifications have also changed to use a WearableExtender() class instead of the WearableNotifications class, as can be seen here in showPageNotifications():
@@ -151,3 +153,3 @@ public class MainActivity extends ActionBarActivity { PendingIntent.getActivity(this, notificationId+1, viewIntent1, 0); - NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this) + Notification notification1 = new NotificationCompat.Builder(this) .addAction(R.drawable.ic_action_done, "Returned", viewPendingIntent1) @@ -155,5 +157,4 @@ public class MainActivity extends ActionBarActivity { .setContentText("You have " + numOverdue + " books due at the library") - .setSmallIcon(R.drawable.ic_launcher); - Notification notification1 = new WearableNotifications.Builder(builder1) - .addPages(extras) + .setSmallIcon(R.drawable.ic_launcher) + .extend(new NotificationCompat.WearableExtender().addPages(extras)) .build();
Conclusion
If you want to download the final source code of showStackNotifications() and showPageNotifications(), you can download the MainActivity.java file. You can build this file easily by creating a new project in Android Studio, adding the support library, and then copying in this MainActivity.java.
As you can see, porting this previous code over to the latest Android
Wear SDK is really easy! It should take you hardly any time at all to
get your experimental applications ported over and ready for publishing
on the Google Play!
Wear SDK is really easy! It should take you hardly any time at all to
get your experimental applications ported over and ready for publishing
on the Google Play!
0 comments:
Post a Comment
Thank you very much for your comments ....................... Please stay with this blog