-1

Actually i am trying to make an android app to display a text when the battery becomes 50% ,m using text view to display the msg,but while loading the app it gets unfortunately stops.

here is my code:

Java file.

package com.example.abhi.batteryalarm;

import android.content.Intent;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class Battery_Alarm extends ActionBarActivity {
    Intent batteryStatus;
    TextView txtView;
    String hello;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battery__alarm);
        txtView=(TextView)findViewById(R.id.txtView);
        hello="This is my first project";

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        if (level==50)
        {
            txtView.setText(hello);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_battery__alarm, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

activity file:

 <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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Battery_Alarm">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textSize="62sp"
        android:id="@+id/txtView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="125dp" />

</RelativeLayout>

What things i need to do?

3 Answers3

1

Add this,

batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

before,

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

and it will work.

Hardik Chauhan
  • 2,750
  • 15
  • 30
  • now in place of displaying text i want to play a sound how i will proceed? i got this code:MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1); mediaPlayer.start(); but could't understand! please help – Abhishek Singh Jan 16 '15 at 17:12
  • so you are saying that the music is not playing??? cause the code looks fine to me. – Hardik Chauhan Jan 17 '15 at 05:18
0

This is the line which is causing the error :

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

It's because the batteryStatus intent is never initialized and is hence null.

Check out this answer about fetching the battery level : https://stackoverflow.com/a/4047790/1239966

Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
0

You need to register batteryStatus.

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
QArea
  • 4,955
  • 1
  • 12
  • 22
  • thanks!!!! its working now!,,,now suppose i want to run this application in background so that when battery becomes 50% the app will open and display the text!!! is it possible to do???/ – Abhishek Singh Jan 17 '15 at 14:59