بسم الله الرحمن الرحيم
كيفية عرض حالة البطارية + هل الشاحن موصول في الجهاز, برمجياً في تطبيق اندرويد.
قم بإنشاء تطبيق جديد, وأنسخ الأكواد التالية :
main.xml
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".Main" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_margin="30dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout_gravity="center" > <TextView android:id="@+id/tv_percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" android:textColor="#000" android:textSize="20sp" android:text="مجهولة" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="نسبة الشحن : " /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout_gravity="center" > <TextView android:id="@+id/tv_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="نعم" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="الشاحن موصول : " /> </LinearLayout> </LinearLayout>
Main.java
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class Main extends Activity { TextView batteryPercent, ChargingState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); batteryPercent = (TextView) findViewById(R.id.tv_percentage); ChargingState = (TextView) findViewById(R.id.tv_state); getBatteryPercentage(); if(isPhonePluggedIn(getApplicationContext()).compareToIgnoreCase("نعم")== 0) { ChargingState.setText("نعم"); } else ChargingState.setText("لا"); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private void getBatteryPercentage() { BroadcastReceiver batteryLevel = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { context.unregisterReceiver(this); int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int level= -1; if (currentLevel >= 0 && scale > 0) { level = (currentLevel * 100) / scale; } batteryPercent.setText(level + "%"); } }; IntentFilter batteryLevelFilter = new IntentFilter( Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryLevel, batteryLevelFilter); } public static String isPhonePluggedIn(Context context) { boolean charging = false; String result = "لا"; final Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean batteryCharge = status == BatteryManager.BATTERY_STATUS_CHARGING; int chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; if (batteryCharge) charging = true; if (usbCharge) charging = true; if (acCharge) charging = true; if (charging){ result = "نعم"; } return result; } }
أفتح ملف AndroidManifest.xml واعطي التطبيق الصلاحيات التالية :
<uses-permission android:name="android.permission.BATTERY_STATS"/> <uses-permission android:name="android.permission.BROADCAST_STICKY"/>
معاينة:
و السلام عليكم ~
ممتار جدا