Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc 5/4730 Android Wear. Note This is all for Android Wear v5.x – V1.x was very limited by comparison It was very difficult to even create a watchface.

Similar presentations


Presentation on theme: "Cosc 5/4730 Android Wear. Note This is all for Android Wear v5.x – V1.x was very limited by comparison It was very difficult to even create a watchface."— Presentation transcript:

1 Cosc 5/4730 Android Wear

2 Note This is all for Android Wear v5.x – V1.x was very limited by comparison It was very difficult to even create a watchface.

3 devices and emulators You can setup a wear emulator, but you need to connect a physical device/phone to it. – Directions: https://developer.android.com/training/wearable s/apps/creating.html https://developer.android.com/training/wearable s/apps/creating.html – You will need android wear app installed on the device.

4 Notifications For many of the applications, it is as simple a sending a notification to the wearable device – As well as device. Clearing a notification on wearable clears it on the phone as well. – You MUST use the NotificationCompat from the support lib v4 (20 and above) and import the following: import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationCompat.WearableExtender ;

5 Simple notification A simple one looks just like a normal notification – But the user will get on the wearable, and when they swipe left o reveal the Open action, which invokes the intent on the handheld device

6 Simple notification code //create the intent to launch the notiactivity, then the pentingintent. Intent viewIntent = new Intent(this, NotiActivity.class); PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, viewIntent, 0); //Now create the notification. We must use the NotificationCompat or it will not work on the wearable. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Simple Noti").setContentText("This is a simple notification").setContentIntent(viewPendingIntent); // Get an instance of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); // Build the notification and issues it with notification manager. NotificationManager.notify(notificationID, notificationBuilder.build());

7 Simple notification result The notification will look this this and then swipe left to an open button

8 Adding your own button. Same as before, but use the addAction(…) method. – Both the device and wearable will show the action..addAction(R.drawable.ic_action_time, "take Picutre", cameraPendingIntent); – Where the Camera intent launches the camera.

9 Wearable only actions. Allows us to change the notification so it’s different from the device and wearable – Instead of addaction, we use.extend(…) –.extend(new WearableExtender().addAction(action) Where action is already build action – NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action_time,"take a Picutre", cameraPendingIntent).build(); – So the take a picture action only shows on the wearable.

10 Other styles. As with notifications, we can also use a BigTextStyle or InboxStyle (see notification inbox before). – BigTextStyles allows more text room and the notification takes up most of the space on the wearable. BigTextStyle bigStyle = new NotificationCompat.BigTextStyle(); bigStyle.bigText(eventDescription); – And then in the builder.setStyle(bigStyle)

11 Voice Actions If the wear device accepts voice input, then you can also add to your notifications. – The user will talk to the wearable, because there is no keyboard. Uses the RemoteInput class, via an intent and add it as an action. – This intent can be captured via the onCreate or onNewIntent Note this can also be done with a broadcast receiver and intent-filter not shown here.

12 Voice Actions (2) For the notification: – With need id/key for this, so using this: String EXTRA_VOICE_REPLY = "extra_voice_reply"; – create the remote input part for the notification. RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel("Reply").build(); – Create the reply action and add the remote input NotificationCompat.Action action = new notificationCompat.Action.Builder( R.drawable.ic_action_map, "Reply", replyPendingIntent).addRemoteInput(remoteInput).build(); – And add the action to the notification as before..extend(new WearableExtender().addAction(action)

13 Voice Actions example

14 Voice Actions (3) Now you get the intent in your activity and pull out the string of text. It uses the Id/Key from before. Bundle remoteInput = RemoteInput.getResultsFromIntent(getIntent()); if (remoteInput != null) { info = remoteInput.getCharSequence( EXTRA_VOICE_REPLY ).toString(); }

15 Example code WearNotiDemo – Shows the code via a subroutine for each of the notifications that I listed. You can run this via a wearable emulator and phone or with a wearable and phone. – Note that the wearable emulator will need the physical keyboard present checked, because there is no voice, you have to type.

16 Wearable Apps This is a little bit of gmagic here. – Much of there api are undocumented and are sort of described in there training material. There a document for the API, but it only tells you the names, parameters, and return values of the API. But NOT actually what it does… really helpful. Also not setup for eclipse. – Github for this lecture has the wearable library needed.

17 Wearable Apps First issue: Round or Square?

18 Round or Square? Both! In the main activity layout your can use WatchViewStub Where the rectLayout and roundLayout point to layouts you can created for each device. – Likely they will have the same Widgets/IDs for each layout, but the way it looks will be different to deal with square or round.

19 Round or Square? Both! (2) Rect.xml Round.xml

20 Round or Square? Both! (2) In the activity, OnCreate: setContentView(R.layout.activity_main); WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { mTextView = (TextView) stub.findViewById(R.id.text); }}); In the onLayoutInflated, we know which layout has been inflated and we use stub to find the widgets and setup like normal. But I’m skipping a lot of different layout and “new” widgets that you have access to via the wearable library. – Many are new to lollipop, in google glass as well. Like cards. Some widgets “sort of” described in the training. Others are not.

21 Example code DemoWearApp – Is my code based on DemoWearApp, where you can a random number (click the checkmark for another number).

22 Creating your Wearable Application Andriod studio 1.1.0 has a template to create a wear app. – It will create a default round and square template for you. – To create an installer without signing your app, follow the directions on the next couple of slides.

23 Wrapper app To install it on the physical device you will need a wrapper activity to install on the phone. – Requires google-play-service-lib see http://developer.android.com/google/play- services/setup.html#Install to import it into your work space. http://developer.android.com/google/play- services/setup.html#Install

24 Wrapper app This is a shell app. – First take note of 3 things from the wearable app. – Package name, versionCode, and VersionName We will need all three.

25 – So create a new project. The Package name must match the wearable app. Uncheck the create activity, not needed. – Delete the following if they exist: res/menu directory and any layout/ files if they exist. Create res/raw and res/xml directories. Copy the apk from the wearable application See /bin/ directory of that project. – And copy into res/raw directory. Rename to all lower case, no special characters. Example: weardemo1.apk

26 Wrapper app xml file Xml/wearable_app_desc.xml file – This describe the apk to be installed onto the device. 2 1.1 weardemo1 – This match the androidManifest.xml information for the wearable app. And weardemo1 is the filename of the apk in raw/

27 Wrapper app androidManifest.xml This file will look something like this. No activity is listed, because it not needed. The 4 lines in red are required.

28 Wrapper app finally! “compile” and install it on the phone connected to the wearable device. Not nothing will actually happen on the phone, because there is no activity. – Now check in the start section of the wearable for the name of you wearable app. Hopefully you tested it thoroughly on the emulator, so it doesn’t just crash!

29 WatchFaces Studio will provide you with two possible watch face apps. – Digital and analog. From there you can change them up as needed, or create your own. – You watchface must deal with ambient mode. There is where the screen is “darkened” and save battery life. Normally remove all color and changes over to 1 minute update. – Otherwise the layout and code is very similar to a standard android app. Very helpful: http://developer.android.com/training/wearables/watc h-faces/index.html http://developer.android.com/training/wearables/watc h-faces/index.html

30 WatchFaces (2) 3 example watch face are provided – BeerWatchFace Doesn’t show the time, instead “Beer time” – Yellow in normal mode, gray in Ambient mode. » Show the min needed to get a watch face working. » Only updates once a minute. – Digital Shows digital time, – Normal mode, shows seconds, ambient mode just hour and minute

31 WatchFaces (3) – Batman watchface Show date and time, plus a graphic – In ambient mode, seconds are not shown and graphic is gray.

32 Data communication. The device and wear can send messages via the DataLayer, which comes from the GooglePlay services. – In the build.gradle for both wear and mobile compile 'com.google.android.gms:play-services-wearable:6.5.87‘ (or current version) – To send a message You will need build a GoogleApiClient, that includes the wearable API and the necessary listeners. – Onconnected, onconnectionsuspeneded, and onconnectionfailed. – And you send the message via your “message path”, which I called, “/message_path”, but you can create your own path name.

33 Sending a message Build a new GoogleApiClient that includes the Wearable API googleClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build(); Then you must use a thread to send the message, since this is a blocking call. MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes()).await(); – See the code for the whole thread.

34 Receiving a message Requires a WearableListenerService Simple service that has a onMessageReceived method. – Check to see if it’s on your “message path” – Then deal with the message. In my code, it uses a local broadcast receiver to send a message to the activity code, so it can be displayed on the wearable and device screens.

35 Important note Both the mobile and wear code MUST have the same applicationId in their build.gradle files – Otherwise it won’t work.

36 Example code There is an WearableDatalayer example that sends and receives messages between a “phone” device and wearable device. Very useful references. http://android-wear- docs.readthedocs.org/en/latest/sync.html http://android-wear- docs.readthedocs.org/en/latest/sync.html https://github.com/LarkspurCA/WearableMessage There are more references in the project as well.

37 References Android’s training site – http://developer.android.com/training/building- wearables.html http://developer.android.com/training/building- wearables.html – https://developer.android.com/training/wearables/apps/i ndex.html https://developer.android.com/training/wearables/apps/i ndex.html – http://developer.android.com/design/wear/principles.htm l http://developer.android.com/design/wear/principles.htm l https://medium.com/@tangtungai/how-to-develop- and-package-android-wear-app-using-eclipse- ef1b34126a5d https://medium.com/@tangtungai/how-to-develop- and-package-android-wear-app-using-eclipse- ef1b34126a5d http://blog.benjamin-cabe.com/2014/07/04/how-to- setup-eclipse-for-android-wear-development http://blog.benjamin-cabe.com/2014/07/04/how-to- setup-eclipse-for-android-wear-development

38 Q A &


Download ppt "Cosc 5/4730 Android Wear. Note This is all for Android Wear v5.x – V1.x was very limited by comparison It was very difficult to even create a watchface."

Similar presentations


Ads by Google