Functionality to shut down or reboot mobile application programmatically through Flutter

78 Views Asked by At

To power off or reboot my phone I have to press the power button.

So, I started working on an app that will allow you to shut down the mobile with a click. But after searching I realized that Android doesn't allow apps to shut down or reboot mobile.

Can I trigger the system notification displaying the shutdown and reboot options (this is displayed when we hold the power button for a while) through the Flutter application?

Source:

import 'package:flutter/material.dart';

class ButtonWindow extends StatelessWidget {
  const ButtonWindow({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        SizedBox(
            width: 180,
            child: ElevatedButton.icon(
              onPressed: () {},
              label: const Text("Shutdown"),
              icon: const Icon(
                Icons.power_off,
                size: 50.0,
              ),
            )),
        const SizedBox(height: 60.0),
        SizedBox(
            width: 180,
            child: ElevatedButton.icon(
              onPressed: () {},
              label: const Text("Reboot"),
              icon: const Icon(
                Icons.restart_alt,
                size: 50.0,
              ),
            ))
      ]),
    );
  }
}

I have searched for this specific problem on Stack overflow but either the packages mentioned are obsolete or the program doesn't work.

1

There are 1 best solutions below

1
AppSolves On
private void configurePowerButton() {
   Button powerButton = (Button) mLayout.findViewById(R.id.power);
   powerButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           performGlobalAction(GLOBAL_ACTION_POWER_DIALOG);
       }
   });
}

There is a Google Codelab which guides you through the necessary steps on how to show the power menu. However, this code is not written in Dart and you have to write some native code yourself. Maybe there will be a plugin in the future...

Update

There is a flutter_accessibility_service plugin that could provide the power menu feature but I was not able to find it in the plugin's documentation.