Reland: Migrate examples to Android embedding v2 (#44139)
This commit is contained in:
@@ -21,5 +21,10 @@
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<!-- Don't delete the meta-data below.
|
||||
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
|
||||
<meta-data
|
||||
android:name="flutterEmbedding"
|
||||
android:value="2" />
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@@ -13,8 +13,9 @@ import android.os.BatteryManager;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
|
||||
import io.flutter.app.FlutterActivity;
|
||||
import androidx.annotation.NonNull;
|
||||
import io.flutter.embedding.android.FlutterActivity;
|
||||
import io.flutter.embedding.engine.FlutterEngine;
|
||||
import io.flutter.plugin.common.EventChannel;
|
||||
import io.flutter.plugin.common.EventChannel.EventSink;
|
||||
import io.flutter.plugin.common.EventChannel.StreamHandler;
|
||||
@@ -29,44 +30,44 @@ public class MainActivity extends FlutterActivity {
|
||||
private static final String CHARGING_CHANNEL = "samples.flutter.io/charging";
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
GeneratedPluginRegistrant.registerWith(this);
|
||||
new EventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
|
||||
new StreamHandler() {
|
||||
private BroadcastReceiver chargingStateChangeReceiver;
|
||||
@Override
|
||||
public void onListen(Object arguments, EventSink events) {
|
||||
chargingStateChangeReceiver = createChargingStateChangeReceiver(events);
|
||||
registerReceiver(
|
||||
chargingStateChangeReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
}
|
||||
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
|
||||
GeneratedPluginRegistrant.registerWith(flutterEngine);
|
||||
|
||||
@Override
|
||||
public void onCancel(Object arguments) {
|
||||
unregisterReceiver(chargingStateChangeReceiver);
|
||||
chargingStateChangeReceiver = null;
|
||||
}
|
||||
new EventChannel(flutterEngine.getDartExecutor(), CHARGING_CHANNEL).setStreamHandler(
|
||||
new StreamHandler() {
|
||||
private BroadcastReceiver chargingStateChangeReceiver;
|
||||
@Override
|
||||
public void onListen(Object arguments, EventSink events) {
|
||||
chargingStateChangeReceiver = createChargingStateChangeReceiver(events);
|
||||
registerReceiver(
|
||||
chargingStateChangeReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(Object arguments) {
|
||||
unregisterReceiver(chargingStateChangeReceiver);
|
||||
chargingStateChangeReceiver = null;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
new MethodChannel(getFlutterView(), BATTERY_CHANNEL).setMethodCallHandler(
|
||||
new MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("getBatteryLevel")) {
|
||||
int batteryLevel = getBatteryLevel();
|
||||
new MethodChannel(flutterEngine.getDartExecutor(), BATTERY_CHANNEL).setMethodCallHandler(
|
||||
new MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("getBatteryLevel")) {
|
||||
int batteryLevel = getBatteryLevel();
|
||||
|
||||
if (batteryLevel != -1) {
|
||||
result.success(batteryLevel);
|
||||
} else {
|
||||
result.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
}
|
||||
if (batteryLevel != -1) {
|
||||
result.success(batteryLevel);
|
||||
} else {
|
||||
result.notImplemented();
|
||||
result.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
}
|
||||
} else {
|
||||
result.notImplemented();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user