My Code Has 4 Different textviews that display 4 different text, the first 3 display True or false and the last one shows the % of battery
1 Battery charging
2 Battery UsbCharge
3.Battery AcCharge
4.Battery Life
I Have The battery life working but I Do not know how to display the first 3 as true or false. Below is the code I have
Main Activity
package com.example.a000339326.myapplication;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.TxtV1);
TextView tv2 = (TextView) findViewById(R.id.TxtV2);
TextView tv3 = (TextView) findViewById(R.id.TxtV3);
TextView tv4 = (TextView) findViewById(R.id.TxtV4);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent intent = registerReceiver(null, intentFilter);
Intent batteryStatus = registerReceiver(null, intentFilter);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
float percent = (float) (level / scale) * 100;
tv.setText("Battery Life " + String.valueOf(percent));
tv2.setText("Battery Charge " + );
tv3.setText("Battery usbCharge " + );
tv4.setText("Battery acCharge " + );
}
}
Manifest.Xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a000339326.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/lab5"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Receiver Class
package com.example.a000339326.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
public class myReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}