text
stringlengths
1
474
_batteryLevel = batteryLevel;
});
}<code_end>
Finally, replace the build method from the template to
contain a small user interface that displays the battery
state in a string, and a button for refreshing the value.
<code_start>@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: _getBatteryLevel,
child: const Text('Get Battery Level'),
),
Text(_batteryLevel),
],
),
),
);
}<code_end>
<topic_end>
<topic_start>
Step 3: Add an Android platform-specific implementation
Start by opening the Android host portion of your Flutter app
in Android Studio:Start Android StudioSelect the menu item File > Open…Navigate to the directory holding your Flutter app,
and select the android folder inside it. Click OK.Open the file MainActivity.kt located in the kotlin folder in the
Project view.Inside the configureFlutterEngine() method, create a MethodChannel and call
setMethodCallHandler(). Make sure to use the same channel name as
was used on the Flutter client side.
<code_start>import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = "samples.flutter.dev/battery"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
// This method is invoked on the main thread.
// TODO
}
}
}<code_end>
Add the Android Kotlin code that uses the Android battery APIs to
retrieve the battery level. This code is exactly the same as you
would write in a native Android app.First, add the needed imports at the top of the file:
<code_start>import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES<code_end>
Next, add the following method in the MainActivity class,
below the configureFlutterEngine() method:
<code_start>private fun getBatteryLevel(): Int {
val batteryLevel: Int
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
} else {
val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
}
return batteryLevel
}<code_end>
Finally, complete the setMethodCallHandler() method added earlier.
You need to handle a single platform method, getBatteryLevel(),
so test for that in the call argument.
The implementation of this platform method calls the
Android code written in the previous step, and returns a response for both
the success and error cases using the result argument.
If an unknown method is called, report that instead.Remove the following code:
<code_start>MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
// This method is invoked on the main thread.
// TODO
}<code_end>
And replace with the following:
<code_start>MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
// This method is invoked on the main thread.
call, result ->
if (call.method == "getBatteryLevel") {
val batteryLevel = getBatteryLevel()
if (batteryLevel != -1) {
result.success(batteryLevel)
} else {
result.error("UNAVAILABLE", "Battery level not available.", null)
}
} else {
result.notImplemented()
}
}<code_end>
Start by opening the Android host portion of your Flutter app
in Android Studio:Start Android StudioSelect the menu item File > Open…Navigate to the directory holding your Flutter app,
and select the android folder inside it. Click OK.Open the MainActivity.java file located in the java folder in the