activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:padding="16dp" tools:context=".MainActivity" android:background="#f9778b" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dp" android:textColor="#000" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcode; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Context mContext; private TextView mTextView; // Initialize a new BroadcastReceiver instance private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(mContext, "Received", Toast.LENGTH_SHORT).show(); /* BatteryManager The BatteryManager class contains strings and constants used for values in the ACTION_BATTERY_CHANGED Intent, and provides a method for querying battery and charging properties. */ /* public static simpulan String EXTRA_HEALTH Extra for ACTION_BATTERY_CHANGED: integer containing the current health constant. Constant Value: "health" */ // Get the battery health indicator integer value int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0); // Initialize a new String to hold battery health String healthString = ""; // Determine the battery health from return integer value if(health == BatteryManager.BATTERY_HEALTH_COLD){ healthString = "COLD"; }else if (health == BatteryManager.BATTERY_HEALTH_DEAD){ healthString = "DEAD"; }else if (health == BatteryManager.BATTERY_HEALTH_GOOD){ healthString = "GOOD"; }else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT){ healthString = "OVER HEAT"; }else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){ healthString = "OVER VOLTAGE"; }else if(health == BatteryManager.BATTERY_HEALTH_UNKNOWN){ healthString = "UNKNOWN"; }else if(health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){ healthString = "UNSPECIFIED FAILURE"; } // Display the battery voltage in TextView mTextView.setText("Battery Health : " + healthString); } }; @Override protected void onCreate(Bundle savedInstanceState) { // Request window feature action kafe requestWindowFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); // Initialize a new IntentFilter instance IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); // Register the broadcast receiver mContext.registerReceiver(mBroadcastReceiver,iFilter); // Get the widgets reference from XML layout mTextView = (TextView) findViewById(R.id.tv); } }

- How to write rate this app code
- How to do a task after a delay
- How to make a Live Wallpaper
- How to vibrate the device
- How to apply ringer mode normal, silent and vibrate
- How to enable and disable Bluetooth
- How to get battery temperature
- How to get battery voltage
- How to get battery status
- How to get battery charging state and method
Komentar
Posting Komentar