text
stringlengths
1
372
<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 studio
select 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
project view.
next, create a MethodChannel and set a MethodCallHandler
inside the configureFlutterEngine() method.
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;
public class MainActivity extends FlutterActivity {
private static final string CHANNEL = "samples.flutter.dev/battery";
@override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setmethodcallhandler(
(call, result) -> {
// this method is invoked on the main thread.
// TODO
}
);
}
}
<code_end>
add the android java 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.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
<code_end>
then add the following as a new method in the activity class,
below the configureFlutterEngine() method:
<code_start>
private int getBatteryLevel() {
int batteryLevel = -1;
if (version.sdk_int >= VERSION_CODES.LOLLIPOP) {
BatteryManager batteryManager = (batterymanager) getSystemService(BATTERY_SERVICE);
batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
intent intent = new ContextWrapper(getApplicationContext()).
registerReceiver(null, new 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>
(call, result) -> {
// this method is invoked on the main thread.
// TODO
}
<code_end>
and replace with the following:
<code_start>
(call, result) -> {
// this method is invoked on the main thread.
if (call.method.equals("getbatterylevel")) {
int batteryLevel = getBatteryLevel();