flutternativeflutter-method-channel

Reboot mobile programmatically


Hi team i created flutter project and my task is if i press the restart button mobile device need to got restarted but if i press the button it wont works got error in console

error -> either user 10478 nor current process has android.permission.REBOOT

android code for restart

  private void restart() {
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        pm.reboot(null);
    }

I have added the permission in androidmanifest too, but no luck.

do I need to restart my device?


Solution

  • 1

    <uses-permission android:name="android.permission.REBOOT" />
    

    2

    import 'package:permission_handler/permission_handler.dart';
    
    void requestRebootPermission() async {
      var status = await Permission.reboot.request();
      if (status != PermissionStatus.granted) {
        print('Permission denied');
        return;
      }
    }
    

    3

    await requestRebootPermission();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    pm.reboot(null);