Android Wear Part3

As the name implies, Android Wear is an Android-based initiative, which uses Google’s mobile operating system and a dedicated SDK to start the wearable apps. It brings google in a small space in a big way as it is starting with some very popular watch faces.

Today in this tutorial we will see how we can add battery information and edit the text color etc. on our watch face. Until now I have published two posts on Android wear here. You can see them on Android Wear Introduction – Part1 and Android wear Part2 – Creating a Helloworld

Referencing the Part2 of Android wear we will extend the code and will show battery information here in this tutorial. Follow the steps as given:

Step1: Open your workspace which you have created for your android wear app explained in Part2

Step2: To insert battery information on your watch face you will need to create a BroadcastReceiver as we created to show the time. Add below code in your main Activity:

 

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
mBattery.setText(String.valueOf(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) + "%"));
}
};

 

 

Step3: Register your receiver in layoutinflate as we did for time and unregister it in OnDestroy().

Step4: This should display the result as below:

wear1

Step5: If you like it and wish to show this output as you watch preview you can save it to drawable and change the setting in wear manifest as below:

 

<meta-data android:name="com.google.android.clockwork.home.preview" android:resource="@drawable/wear1" />

 

 

Step6: Now we want to edit the color of the text displayed on our watch face to make it more diligently. We will use Ambient Mode for this and will create an another activity to manage this all. So create and write below in watchFaceClass.java:

 

package com.example.myandroidwear;

import android.app.Activity;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.view.Display;

public abstract class watchFaceClass extends Activity implements DisplayManager.DisplayListener {

public abstract void onScreenDim();

public abstract void onScreenAwake();

public void onWatchFaceRemoved(){}

public void onScreenOff(){}

private DisplayManager displayManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(this, null);
}

@Override
protected void onDestroy() {
super.onDestroy();
displayManager.unregisterDisplayListener(this);
}

@Override
public void onDisplayRemoved(int displayId) {
onWatchFaceRemoved();
}

@Override
public void onDisplayAdded(int displayId) {
}

@Override
public void onDisplayChanged(int displayId) {
switch(displayManager.getDisplay(displayId).getState()){
case Display.STATE_DOZE:
onScreenDim();
break;
case Display.STATE_OFF:
onScreenOff();
break;
default:
onScreenAwake();
break;
}
}
}

 

Step7: Your MainActivity.java should look like below till now:

 

package com.example.myandroidwear;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends watchFaceClass {

private TextView mTextView;
private TextView mTime , mBattery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTime = (TextView) stub.findViewById(R.id.watch_time);
mTime.setTextColor(Color.RED);
mBattery = (TextView) stub.findViewById(R.id.watch_battery);
mBattery.setTextColor(Color.RED);
mTimeInfoReceiver.onReceive(MainActivity.this, null);
registerReceiver(mTimeInfoReceiver, INTENT_FILTER);
registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

}
});
}

private final static IntentFilter INTENT_FILTER;
static {
INTENT_FILTER = new IntentFilter();
INTENT_FILTER.addAction(Intent.ACTION_TIME_TICK);
INTENT_FILTER.addAction(Intent.ACTION_TIMEZONE_CHANGED);
INTENT_FILTER.addAction(Intent.ACTION_TIME_CHANGED);
}

private final String TIME_FORMAT_DISPLAYED = "kk:mm a";

private BroadcastReceiver mTimeInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
mTime.setText(
new SimpleDateFormat(TIME_FORMAT_DISPLAYED)
.format(Calendar.getInstance().getTime()));
}
};

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
mBattery.setText(String.valueOf(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) + "%"));
}
};
public void onScreenDim() {
mTime.setTextColor(Color.WHITE);
mBattery.setTextColor(Color.WHITE);
}

public void onScreenAwake() {
mTime.setTextColor(Color.RED);
mBattery.setTextColor(Color.RED);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mTimeInfoReceiver);
unregisterReceiver(mBatInfoReceiver);
}
}

 

 

Step8: Run to check the output below:

wear2

This is all it takes to make a watch face in android wear. I will come back on the Android wear with some more popular and useful topics soon, until then Enjoy reading my blogs here. Check out the last post I posted on Creating ListView with Sections.

Leave a Comment: