I want to get battery level percentage from one activity to another.
I can't get it from "BroadcastReciver" and also I can't extract it from text view.
this is my Broadcast Reciever :
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
float percentage = level/ (float) scale;
int mProgressStatus = (int)((percentage)*100);
bl.setText("" + mProgressStatus + "%");
}
};
And this is how I am extracting it:
public int batt(){
String input = bl.getText().toString();
input = input.replace( "\n", "" );
input = input.replace( " ", "" );
int total = getValue(input);
return total;
}
public int getValue(String line){
int value = 0;
if( line.contains( "+" ) ) {
String[] lines = line.split( "\\+" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value += getValue( lines[i] );
return value;
}
if( line.contains( "-" ) ) {
String[] lines = line.split( "\\-" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value -= getValue( lines[i] );
return value;
}
if( line.contains( "*" ) ) {
String[] lines = line.split( "\\*" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value *= getValue( lines[i] );
return value;
}
if( line.contains( "/" ) ) {
String[] lines = line.split( "\\/" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value /= getValue( lines[i] );
return value;
}
return Integer.parseInt( line );
}