Flutter:没有在通道myChannel上找到getBatteryLevel方法的实现



我使用MethodChannel实现解锁屏幕以查看即将到来的呼叫。当模拟器屏幕打开时,我的其他方法通道,如获取电池水平信息正在工作,它工作完美,但当我锁定屏幕时,我开始得到错误。我的应用程序给出:MissingPluginException(没有实现方法getBatteryLevel上的通道myChannel)

当屏幕关闭时如何激活应用程序的方法通道?

下面是我的代码:

MainActivity.java

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.app.KeyguardManager.KeyguardLock;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.app.KeyguardManager;
import android.content.Context;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "myChannel";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("getBatteryLevel")) {
int batteryLevel = getBatteryLevel();
if (batteryLevel != -1) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}
);
}
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;
}
}

Dart:

const platform = const MethodChannel('myChannel');
Future<int> getBatteryLevel() async {
int batteryLevel;
try {
batteryLevel = await platform.invokeMethod('getBatteryLevel');
} on PlatformException catch (e) {
print("Error: '${e.message}'.");
batteryLevel = -1; // Return -1 if there's an error
}
return batteryLevel;
}

AndroidManifest权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

当你锁定手机时,你的应用程序被暂停,不能在后台做任何工作。为了让你的应用在没有任何用户交互/UI的情况下恢复,你需要一个服务:https://developer.android.com/guide/components/services

相关:

  • https://developer.android.com/guide/components/activities/activity-lifecycle
  • https://github.com/flutter/flutter/issues/76696

相关内容

  • 没有找到相关文章

最新更新