Wednesday 6 August 2014

HB Blog 10: Android wearables basic PageNotification integration.

Wearables are the next big frontier for smart technology.Android Wear is an Android-based initiative, using Google's mobile operating system and a dedicated software development kit to kick-start the wearables space.

Step I :- Set up an Android Wear Virtual Device
   
To set up an Android Wear virtual device(Emulator) on IDE(Eclipse):

    1)Click Tools > Android > AVD Manager.
    2)Click Create....
    3)Fill in the following details for the AVD you want to specify and leave the rest of the fields with their default values:
        AVD Name - A name for your AVD
        Device - Android Wear Round or Square device types
        Target - Android 4.4W - API Level 20
        CPU/ABI - Android Wear ARM (armeabi-v7a)
        Keyboard - Select Hardware keyboard present
        Skin - AndroidWearRound or AndroidWearSquare depending on the selected device type
        Snapshot - Not selected
        Use Host GPU - Selected, to support custom activities for wearable notifications
    4)Click OK.
    5)Start the emulator:
        Select the virtual device you just created.
        Click Start..., then click Launch.
        Wait until the emulator initializes and shows the Android Wear home screen.

Step II :- Create android project and start coding...
//Few code snippets...
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 int notificationId = 001;
        // Build intent for notification content
                Intent viewIntent = new Intent(this, MainActivity.class);
                PendingIntent viewPendingIntent =
                        PendingIntent.getActivity(this, 0, viewIntent, 0);
        
 // Create builder for the main notification
        Builder notificationBuilder =
                new Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Page 1")
                .setContentText("Harshal Benake demo message")
                .setContentIntent(viewPendingIntent);

        // Create a big text style for the second page
        BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
        secondPageStyle.setBigContentTitle("Page 2")
                       .bigText("Harshal Benake demo message BigTextStyle");

        // Create second page notification
        Notification secondPageNotification =
                new NotificationCompat.Builder(this)
                .setStyle(secondPageStyle)
                .build();

        // Add second page with wearable extender and extend the main notification
        Notification twoPageNotification =
                new WearableExtender()
                        .addPage(secondPageNotification)
                        .extend(notificationBuilder)
                        .build();

        // Issue the notification
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId, twoPageNotification);


 Refer the below link for complete sample code:-
 Download Sample Code
 Download Apk File

No comments:

Post a Comment