Flashlight Part II (BatteryStats and Notifications)

FlashLight Part II

(Battery Stats and Notifications)



Picking up from where we left from  Part I, we are going to add some pretty cool design elements to the UI. It's quite simplistic and just shows the battery stats to the user so as to notify him/her about the battery percentage and temperature etc. Also we implement Notifications for the app so it can notify the user about whether the light is on or off. So let's begin with designing part first.

1. Add three TextViews to the layout from PartI

  • Drag and drop three text views in the Graphical Layout of the app (activity_main.xml).
  • Edit the xml as shown below.

App layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back_wall"
tools:context="${packageName}.${activityClass}"
tools:ignore="HardcodedText" >
<TextView
android:id="@+id/textView1"
android:layout_width="200sp"
android:layout_height="150sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="Bat"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="100sp"
android:textStyle="normal" />
<Button
android:id="@+id/button1"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="84dp"
android:background="@drawable/multi_button"
android:gravity="center_vertical|center_horizontal"
android:text="ON"
android:textStyle="bold|normal"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:gravity="center_vertical|center_horizontal"
android:text="Plugged Stat"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginTop="57dp"
android:layout_toRightOf="@+id/textView1"
android:text="Temp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
</RelativeLayout>

2. Changes to the Manifest file

  • In the Manifest file of the previous part we make our activity launchMode as singleTask. This is required as we are going to implement the same activity as the Pending Intent to our Notification.
  • In the activity tag of the manifest file add the line -  android:launchMode="singleTask".

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.roadtodroid.flashlight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.roadtodroid.flashlight.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
view raw Manifest.xml hosted with ❤ by GitHub

3. Display a Notification

  • To display a notification when the on button is pressed use the following function in the OnClickListner of the button.
  • The function uses the NotificationCompat.Builder class to create a notification.
  • The Pending Intent class is used to create a link to the same class (MainActivity)  when the notification is clicked on.
  • Finally the number_notifications variable is used to count the number of notification are there.
protected void Displaynotification(){
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this);
mBuilder.setContentTitle("FlashLight ON");
mBuilder.setContentText("Click to turn off");
mBuilder.setTicker("Flash light on");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
++number_notifications;
mBuilder.setAutoCancel(true);
PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), Intent.FILL_IN_ACTION);
mBuilder.setContentIntent(pending);
myNotifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotifManager.notify(1,mBuilder.build());
}

4. Cancel a Notification or all of them

protected void cancelNotification() {
for(int id=1;id<=number_notifications;id++)
{
myNotifManager.cancel(id);
}
myNotifManager.cancelAll();
}

5. Create a Broadcast Receiver for battery stats


private BroadcastReceiver BatteryReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int plugged=intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
if(level>=0 && scale>=0){
TextView perc=(TextView)findViewById(R.id.textView1);
perc.setText(String.valueOf(level*100/scale) + "%" );
TextView plug= (TextView)findViewById(R.id.textView2);
if(plugged==0){
plug.setText("\nOn Battery");
}else{
plug.setText("\nPlugged In");
}
TextView temp =(TextView)findViewById(R.id.textView3);
temp.setText(String.valueOf(temperature/10) + " *C");
}
}
};

6. MainActivity.java file 


package com.roadtodroid.flashlight;
import java.security.Policy.Parameters;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.*;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.AsyncTask;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
private Button flash;
private Boolean flag = true;
private Camera mCamera;
private NotificationManager myNotifManager;
private int number_notifications=0;
private MediaPlayer myMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
flash =(Button)findViewById(R.id.button1);
flash.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if( getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) == false){
Toast.makeText(getApplicationContext(), "Flash not available", Toast.LENGTH_LONG).show();
finish();
}else{
if(flag == true){
//new DoSomethingOnBTNpress().execute("");
sound();
on();
get_camera();
Displaynotification();
}else{
//Toast.makeText(getApplicationContext(), "Flash off", Toast.LENGTH_LONG).show();
sound();
off();
release_cameara();
cancelNotification();
}
}
}
});
this.registerReceiver(this.BatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
off();
flag=false;
sound();
mCamera.release();
flag=true;
}
private void on(){
flag=false;
flash.setTextColor(Color.RED);
flash.setText("OFF");
flash.setBackgroundResource(R.drawable.multi_button_red);
}
private void off(){
flag=true;
flash.setTextColor(Color.WHITE);
flash.setText("ON");
flash.setBackgroundResource(R.drawable.multi_button);
}
private void get_camera(){
mCamera = Camera.open();
Camera.Parameters params = (android.hardware.Camera.Parameters) mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters((android.hardware.Camera.Parameters) params);
mCamera.startPreview();
}
private void release_cameara(){
mCamera.stopPreview();
mCamera.release();
}
private void sound(){
if(flag==true)
myMediaPlayer= MediaPlayer.create(getApplicationContext(), R.raw.snd1);
else
myMediaPlayer= MediaPlayer.create(getApplicationContext(), R.raw.snd2);
//start the sound
myMediaPlayer.start();
//oncomplete listner
myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
myMediaPlayer.release();
}
});
}
protected void Displaynotification(){
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this);
mBuilder.setContentTitle("FlashLight ON");
mBuilder.setContentText("Click to turn off");
mBuilder.setTicker("Flash light on");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
++number_notifications;
mBuilder.setAutoCancel(true);
PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class).putExtra("resume", true), Intent.FILL_IN_ACTION);
mBuilder.setContentIntent(pending);
myNotifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotifManager.notify(1,mBuilder.build());
}
protected void cancelNotification() {
for(int id=1;id<=number_notifications;id++)
{
myNotifManager.cancel(id);
}
myNotifManager.cancelAll();
}
private BroadcastReceiver BatteryReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int plugged=intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
if(level>=0 && scale>=0){
TextView perc=(TextView)findViewById(R.id.textView1);
perc.setText(String.valueOf(level*100/scale) + "%" );
TextView plug= (TextView)findViewById(R.id.textView2);
if(plugged==0){
plug.setText("\nOn Battery");
}else{
plug.setText("\nPlugged In");
}
TextView temp =(TextView)findViewById(R.id.textView3);
temp.setText(String.valueOf(temperature/10) + " *C");
}
}
};
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(this.BatteryReceiver);
if(myNotifManager!=null) {
myNotifManager.cancelAll();
cancelNotification();
}
if(mCamera != null)
mCamera.release();
}
}


Download Code

SHARE

Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment