text stringlengths 1 372 |
|---|
setState(() { |
_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 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 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 |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.