我想知道使用的设备是不是电视。我正在使用flutter_device_type包,但它只能检测平板电脑,并将任何其他设备视为手机。
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
bool isTV = androidInfo.systemFeatures.contains('android.software.leanback')
String getDeviceType() {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 600 ? 'phone' :'tablet';
}
val uiModeManager = getSystemService(UI_MODE_SERVICE) as UiModeManager
if (uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device")
} else {
Log.d(TAG, "Running on a non-TV Device")
}
因此,正确的选择是使用本地代码,并使用平台通道将其连接到Flutter。
package [your package here]
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = "android.tv"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if(!isLeanbackLauncherDevice()) {
result.success(false)
} else {
result.success(true)
}
}
}
fun isLeanbackLauncherDevice(): Boolean {
val pm = this.getPackageManager()
if (pm.hasSystemFeature("PackageManager.FEATURE_LEANBACK")) {
return true
} else {
return false
}
}
}
import 'package:flutter/services.dart';
const platform = MethodChannel('android.tv');
Future<bool> isAndroidTv() async {
final bool isTv = await platform.invokeMethod('isLeanbackLauncherDevice');
return isTv;
}
android.software.leanback
。 - Tien Do Nam