gpt4 book ai didi

Flutter 应用程序 - 能够关闭屏幕

转载 作者:行者123 更新时间:2023-12-04 09:57:43 24 4
gpt4 key购买 nike

Android 上的 Flutter 应用程序有什么方法可以锁定屏幕吗?我说的是用户按下手机电源按钮时发生的相同操作。

最佳答案

目前flutter上是没有的,必须借助native实现。按照以下实现。

flutter 代码:

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('flutter.native/powerOff');

Future<void> responseFromNativeCode() async {
try {
await platform.invokeMethod('powerOff');
} on PlatformException catch (e) {
print("Failed to Invoke: '${e.message}'.");
}
}

@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: RaisedButton(
child: Text('Turn Off'),
onPressed: responseFromNativeCode,
),
),
);
}
}

Java 代码:

<强>1。主事件

    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 android.content.Context;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "flutter.native/powerOff";
static final int RESULT_ENABLE = 1;
DevicePolicyManager deviceManger;
ComponentName compName;

@Override
public void configureFlutterEngine(FlutterEngine flutterEngine) {
compName = new ComponentName(this, DeviceAdmin.class);
deviceManger = (DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE);
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("powerOff")) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You should enable the app!");
startActivityForResult(intent, RESULT_ENABLE);
}
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK) {
deviceManger.lockNow();
}
return;
}
}
}

<强>2。收件人代码

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
Toast.makeText(context, "Enabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Toast.makeText(context, "Disabled", Toast.LENGTH_SHORT).show();
}
}

<强>3。 AndroidMenifest.xml

 <receiver
android:name=".DeviceAdmin"
android:description="@string/app_description"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/policies" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>

4.policies.xml - 添加到res/xml文件夹

<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>

最后,在receiver需要的android工程的strings.xml文件中添加String值

关于Flutter 应用程序 - 能够关闭屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61889443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com