Showing posts with label Battery. Show all posts
Showing posts with label Battery. Show all posts

Monday, April 12, 2010

Perfect Mobile Platform

What would make up the perfect mobile platform? What characteristics should it posses? Here are some of them, in no specific order:
  • User Interface/Experience - a rich user interface that provides a great user experience. When using the phone, everything should come naturally, with ease, and should have the WOW factor that makes you never want to leave the phone out of your hands. Doing common tasks should not take more than a few clicks. 
  • Application Ecosystem - Having many applications to chose from is not as important as having quality applications, those that satisfy a particular need, like finding a place to eat, checking your email, or playing Doom. Integrating location, social networking, and sensors is a must.
  • Internet Browsing - surf the Web from anywhere, on the go. A mobile browser should be able to render pages properly and fast, navigation should be done with ease, data should be compressed as to save bandwidth, and use the latest web technologies (HTML5, CSS, JavaScript, etc).
  • Battery Life - it better not leave me hanging after a full day of use, or in the middle of a call, or while searching for a place to eat using the GPS. 
  • Development Ecosystem - provide a rich set of APIs that can access all the features of the phone, and can provide the best user experience. Provide tools that can be used to make it easier to write apps. Publishing an application should not involve much hassle. 
  • Openness - here I refer to not only an open and free platform, but to a platform that allows any mobile technology to work on it.
  • Enterprise - features such as security, integration with email/calendar/notes/contacts servers, messaging, to name just a few. 
There is no one platform that satisfies all these needs better than any other platform. What is needed is a platform that has the user experience of iPhone and Android, the application ecosystem of iPhone (and Android very soon), the internet browsing of iPhone and Android (mostly any WebKit-based browsers), the battery life of the Blackberry, the development ecosystem of Android and iPhone, the openness of Android, and the enterprise characteristics of Blackberry. 

Will we ever have such a platform? I really doubt it. 

Sunday, February 7, 2010

Getting the Battery Level in Android using the SDK

Finding out the battery level using the Android SDK is not quite an easy task because this functionality is not documented, which means it can change at any point in time. Well, after reading a little more about the ACTION_BATTERY_CHANGED Intent and about the BatteryManager class, starting with API Level 5, you have all the information needed to get the status on your phone's battery. The BatteryManager class contains some String values that will give you information about the current battery level and the scale, constants such as EXTRA_LEVEL and EXTRA_SCALE. If you were to work with an API version older than 2.0 (< 5), then it becomes a little more difficult to find the right information as it is not documented in the APIs as far as I could tell.

For now, here is how you can find out:
  • Create an IntentFilter that matches the Intent.ACTION_BATTERY_CHANGED action.
  • Create a BroadcastReceiver that will be called with a broadcast intent.
  • Inside onReceive, retrieve data based on the "level" item (battery level) for version < 5, or use BatteryManager.EXTRA_LEVEL for versions >= 5.
  • Register the receiver that will be called with the broadcast intent that signals a battery change 
Here is the BatteryLevelActivity:
package edu.fau.csi.battery;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

/**
 * Used for finding the battery level of an Android-based phone.
 * 
 * @author Mihai Fonoage
 *
 */
public class BatteryLevelActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView batterLevel;

    @Override
    /**
     * Called when the current activity is first created.
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        batterLevel = (TextView) this.findViewById(R.id.batteryLevel);
        batteryLevel();
    }

    /**
     * Computes the battery level by registering a receiver to the intent triggered 
     * by a battery status/level change.
     */
    private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra("level", -1);
                int scale = intent.getIntExtra("scale", -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }
    
}
The battery.xml layout file is next:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  
    <TextView         
        android:id="@+id/batteryLevel"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center_vertical|center_horizontal"
        android:textSize="50dip"> 
    </TextView>
    
</LinearLayout>


As I mentioned in the beginning, if you are using Android 2.0 or higher, you can make the following changes to the batteryLevel method of the BatterLevelActivity class:
    /**
     * Computes the battery level by registering a receiver to the intent triggered 
     * by a battery status/level change.
     */
    private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }
The code for getting the battery level was inspired from the Twisty project.

Enjoy!