Ad

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

Sunday, November 17, 2013

Android Detect Battery Info.

The app simple detect battery level, voltage, temperature, technology, charging status, and health of the battery.




Activity


 public class MainActivity extends Activity {

    private TextView batteryLevel, batteryVoltage, batteryTemperature,
            batteryTechnology, batteryStatus, batteryHealth;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        batteryLevel = (TextView) findViewById(R.id.batterylevel);
        batteryVoltage = (TextView) findViewById(R.id.batteryvoltage);
        batteryTemperature = (TextView) findViewById(R.id.batterytemperature);
        batteryTechnology = (TextView) findViewById(R.id.batterytechology);
        batteryStatus = (TextView) findViewById(R.id.batterystatus);
        batteryHealth = (TextView) findViewById(R.id.batteryhealth);

        this.registerReceiver(this.myBatteryReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
    }

    private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub

            if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
                batteryLevel.setText("Level: "
                        + String.valueOf(arg1.getIntExtra("level", 0)) + "%");
                batteryVoltage
                        .setText("Voltage: "
                                + String.valueOf((float) arg1.getIntExtra(
                                        "voltage", 0) / 1000) + "V");
                batteryTemperature.setText("Temperature: "
                        + String.valueOf((float) arg1.getIntExtra(
                                "temperature", 0) / 10) + "c");
                batteryTechnology.setText("Technology: "
                        + arg1.getStringExtra("technology"));

                int status = arg1.getIntExtra("status",
                        BatteryManager.BATTERY_STATUS_UNKNOWN);
                String strStatus;
                if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    strStatus = "Charging";
                } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                    strStatus = "Dis-charging";
                } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                    strStatus = "Not charging";
                } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
                    strStatus = "Full";
                } else {
                    strStatus = "Unknown";
                }
                batteryStatus.setText("Status: " + strStatus);

                int health = arg1.getIntExtra("health",
                        BatteryManager.BATTERY_HEALTH_UNKNOWN);
                String strHealth;
                if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
                    strHealth = "Good";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
                    strHealth = "Over Heat";
                } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
                    strHealth = "Dead";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                    strHealth = "Over Voltage";
                } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
                    strHealth = "Unspecified Failure";
                } else {
                    strHealth = "Unknown";
                }
                batteryHealth.setText("Health: " + strHealth);

            }
        }

    };
}

XML activity_main.xml

<?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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"
    />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Info."
    />
<TextView 
    android:id="@+id/batterylevel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Level:"
    />
<TextView 
    android:id="@+id/batteryvoltage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Voltage:"
    />
<TextView 
    android:id="@+id/batterytemperature"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Temperature:"
    />
<TextView 
    android:id="@+id/batterytechology"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Technology:"
    />
<TextView 
    android:id="@+id/batterystatus"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Status:"
    />
<TextView 
    android:id="@+id/batteryhealth"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Health:"
    />
</LinearLayout>

Thursday, December 20, 2012

Android Toast notification show Different Gravity


Toast notify we can show in different gravity like "TOP", "RIGHT", "LEFT", "BOTTOM",etc.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       
        Toast t = Toast.makeText(MainActivity.this, "TOP | RIGHT", Toast.LENGTH_LONG);
        t.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
        t.show();
       
        t = Toast.makeText(MainActivity.this, "CENTER", Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
       
        t = Toast.makeText(MainActivity.this, "BOTTOM | LEFT", Toast.LENGTH_LONG);
        t.setGravity(Gravity.BOTTOM|Gravity.LEFT, 0, 0);
        t.show();
       
        t = Toast.makeText(MainActivity.this, "CENTER | LEFT", Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER|Gravity.LEFT, 0, 0);
        t.show();
    
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


Thursday, November 1, 2012

Android Google Mapview

main.xml

<?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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Waiting for location..."
    android:id="@+id/lblLocationInfo"/>
  <!-- <1> -->
 
 
  
<com.google.android.maps.MapView android:id="@+id/mapview"
   android:apiKey="0dv9akiSW0I8Vvu5Ht_ScUSAEA6tngHm7Sta1ZA"      
    android:enabled="true"
   
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

   
</LinearLayout>



map.java

public class map extends MapActivity implements LocationListener { //<1>
     
      private static final String TAG = "LocationActivity";

      LocationManager locationManager; //<2>
      Geocoder geocoder; //<3>
      TextView locationText;
      MapView map; 
      MapController mapController; //<4>
     
     
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
        map = (MapView)this.findViewById(R.id.mapview);
        map.setBuiltInZoomControls(true);
       
        mapController = map.getController(); //<4>
        mapController.setZoom(16);
       
        locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); //<2>
       
        geocoder = new Geocoder(this); //<3>
       
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //<5>
        if (location != null) {
          Log.d(TAG, location.toString());
          this.onLocationChanged(location); //<6>
        }
      }

     
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
      }

     
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this); //<8>
      }

     
      public void onLocationChanged(Location location) { //<9>
        Log.d(TAG, "onLocationChanged with location " + location.toString());
        String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
                      location.getLongitude(), location.getAltitude(), location.getBearing());
        this.locationText.setText(text);
       
        try {
          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
          for (Address address : addresses) {
            this.locationText.append("\n" + address.getAddressLine(0));
          }
         
          int latitude = (int)(location.getLatitude() * 1000000);
          int longitude = (int)(location.getLongitude() * 1000000);

          GeoPoint point = new GeoPoint(latitude,longitude);
          mapController.animateTo(point); //<11>
         
        } catch (IOException e) {
          Log.e("LocateMe", "Could not get Geocoder data", e);
        }
      }

   
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

   
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
       
    }

   
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
       
    }

   
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
       
    }

    }

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-library android:name="com.google.android.maps" />