Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class Main {
    /**
     * Gets the battery info of device, and then writes it to a file.
     * 
     * @param context the context of application
     */
    public static String getBatteryUsageInfo(Context context) {
        Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int rawlevel = batteryIntent.getIntExtra("level", -1);
        double scale = batteryIntent.getIntExtra("scale", -1);
        return String.format("Device has %s of %s battery left", rawlevel, scale);
    }
}