Android 插入壁式充电器时自动启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26294258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:12:18  来源:igfitidea点击:

Auto boot when wall charger is plugged

androidsu

提问by Rohit

I'm Developing an Android Application where it requireds 3 things:-

我正在开发一个 Android 应用程序,它需要 3 件事:-

  1. To keep the App up an running for specific time and then phone will be off (Working Fine)
  2. When phone get charging from outlet, I want phone to power up automatically without hitting any power button. (Not Working with wall Socket, but working when connected to USB cable via Laptop).
  3. After boot my app should start working Automatically (Working Fine)
  1. 保持应用程序运行特定时间,然后手机将关闭(工作正常)
  2. 当手机从插座充电时,我希望手机在不按任何电源按钮的情况下自动开机。(不适用于墙上插座,但在通过笔记本电脑连接到 USB 电缆时工作)。
  3. 启动后我的应用程序应该开始自动工作(工作正常)

You Must know :- My phone is Moto E (rooted) and want 2nd step to be done. Tried some codes but that does not work on moto E. When Connected with USB it gives 2 as response and when connected with Wall socket charger it says 1

您必须知道:- 我的手机是 Moto E(已植根)并且想要完成第二步。尝试了一些代码,但在 moto E 上不起作用。当与 USB 连接时,它给出 2 作为响应,当与壁式插座充电器连接时,它显示 1

Any help will be Appreciated

任何帮助将不胜感激

P.S :- Auto Boot working with USB cable connected with Laptop but not with Socket Charger

PS :- 使用 USB 线连接笔记本电脑但不连接插座充电器时自动启动

Update-1: 1- Found fastboot oem off-mode-charge 0working with Nexus 7 but not on Moto e.

更新 1:1- 发现可以fastboot oem off-mode-charge 0与 Nexus 7 一起使用,但不能在 Moto e 上使用。

2- Moto e boots when connected to Router (USB Dongle Port)

2- Moto e 连接到路由器时启动(USB 加密狗端口)

采纳答案by Rohit

At last I got the solution, you can achieve this by deleting system/bin/charge_only_modefile. Please do that at your own risk and before deleting have backup of that file. I got the desired result that was boot when its connected to wall charger and now its working fine. All the best!

最后我得到了解决方案,您可以通过删除system/bin/charge_only_mode文件来实现这一点。请自行承担风险,并在删除之前备份该文件。我得到了想要的结果,当它连接到壁式充电器时启动,现在它工作正常。祝一切顺利!

回答by Mad Scientist Moses

Moto e4 and Pixel 2 XL:

Moto e4 和 Pixel 2 XL:

Get your device into the bootloader (fastboot) and run the following command from a computer connected over USB with Android Tools:

将您的设备放入引导加载程序 (fastboot) 并从通过 USB 连接的计算机上运行以下命令并使用 Android 工具:

fastboot oem off-mode-charge 0

fastboot oem off-mode-charge 0

回答by dazza5000

I was able to get it to work by updating the init.rcfile

我能够通过更新init.rc文件来让它工作

I found the on chargertrigger and added the following lines below it:

我找到了on charger触发器并在它下面添加了以下几行:

setprop ro.bootmode "normal"
setprop sys.powerctl "reboot"

The entire trigger block ends up looking like this

整个触发器块最终看起来像这样

on charger
    class_start charger
    setprop ro.bootmode "normal"
    setprop sys.powerctl "reboot"

You then need to repack and flash the boot image created after the updates.

然后,您需要重新打包并刷新更新后创建的引导映像。

Connect the device over USB

通过 USB 连接设备

Power on device and get to bootloader mode

打开设备电源并进入引导加载程序模式

adb reboot bootloader 

To flash boot image execute the following command while in fastboot

要闪存启动映像,请在 fastboot 中执行以下命令

fastboot flash boot new-boot.img

Note: This fix will cause the device to reboot when its plugged in even when shutting it off using the power button or software shutdown.

注意:此修复将导致设备在插入时重新启动,即使使用电源按钮或软件关闭将其关闭。

Source: https://forum.xda-developers.com/showthread.php?p=77766638#post77766638

来源:https: //forum.xda-developers.com/showthread.php?p=77766638#post77766638

You can see the commit that contains these changes for my project here:

您可以在此处查看包含我的项目的这些更改的提交:

https://github.com/darran-kelinske-fivestars/mkbootimg_tools/commit/c8633a2ec2e1924272fd16a8ed7fa7414bb65544#diff-d0007d763b44198f7c22e2c19609d5bbR606

https://github.com/darran-kelinske-fivestars/mkbootimg_tools/commit/c8633a2ec2e1924272fd16a8ed7fa7414bb65544#diff-d0007d763b44198f7c22e2c5bb609d

回答by Frank N Stein

I also tried replacing charge_only_mode with a sh script that rebooted the phone but only got a red circle with the M (on a Motorola Bionic). Changing the script to the below got it working...Now I get the red circle with the M for a few seconds, then a blank screen, the another red circle with the M, and it boots on up.

我还尝试用重新启动手机的 sh 脚本替换charge_only_mode,但只有一个带有M的红色圆圈(在摩托罗拉仿生上)。将脚本更改为以下内容使其正常工作......现在我得到了带有 M 的红色圆圈几秒钟,然后是一个空白屏幕,另一个带有 M 的红色圆圈,然后它启动了。

#!/system/bin/sh
su -c "/system/bin/reboot -n outofcharge"

回答by Hadi

On my device Lenovo K7000-Plus, the file need to be modified is kpoc_chargerlocated at /system/bin.

在我的设备 Lenovo K7000-Plus 上,需要修改的文件kpoc_charger位于/system/bin.

Ipod file not working on my phone which using Android 6.0 ROM, but kpoc_charger works pefectly.

Ipod 文件在我使用 Android 6.0 ROM 的手机上不起作用,但 kpoc_charger 工作正常。

Regards

问候

Hadi

哈迪

回答by Abhishek Verma

For Lenovo A2010 phone,following worked:

对于联想 A2010 手机,以下工作:

  1. Use file manager phone app from playstore like Total commander(on rooted phone) to goto folder /system/bin/
  2. Copy file kpoc_charger and paste it there as kpoc_charger.bak
  3. Now edit the original file kpoc_charger using total-commander, replace all lines with following code:

    #!/system/bin/sh
    /system/bin/reboot
    
  4. Save it, goto properties and change UID:0 root, GID:2000 shell and permission as 755 (same as properties of other files in /system/bin folder).
  5. Now shutdown phone and plug to charger.
  6. Bazinga!!!! battery icon shows for a sec but phone sucessfully boots into os.
  1. 使用 Playstore 中的文件管理器手机应用程序,如 Total Commander(在有根手机上)转到文件夹 /system/bin/
  2. 复制文件 kpoc_charger 并将其粘贴为 kpoc_charger.bak
  3. 现在使用 total-commander 编辑原始文件 kpoc_charger,用以下代码替换所有行:

    #!/system/bin/sh
    /system/bin/reboot
    
  4. 保存它,转到属性并更改 UID:0 root、GID:2000 shell 和权限为 755(与 /system/bin 文件夹中其他文件的属性相同)。
  5. 现在关闭手机并插入充电器。
  6. 巴辛加!!!!电池图标显示一秒钟,但手机成功启动到操作系统。

回答by Amir Dashti

I found another way for this (thanks to DavidThompson256 http://forum.xda-developers.com/showthread.php?t=1187631)

我为此找到了另一种方法(感谢 DavidThompson256 http://forum.xda-developers.com/showthread.php?t=1187631

First make sure your phone is rooted (which I found iRoot very good for this), then install RootExplorer.apk (or similar) on your phone.

首先确保您的手机已植根(我发现 iRoot 对此非常有用),然后在您的手机上安装 RootExplorer.apk(或类似的)。

Try to edit "/system/bin/playlpm" and replace its content with following commands: (do not forget to make a backup first).

尝试编辑“/system/bin/playlpm”并用以下命令替换其内容:(不要忘记先进行备份)。

#!/system/bin/sh
/system/bin/reboot

(I know the content is in binary, simply remove them and write those two lines and save the file)

(我知道内容是二进制的,只需删除它们并写入这两行并保存文件)

NOTE: When you modify that file, no changes will be applied on its permissions but if you are making another file remember to set permissions exactly as it was.

注意:当您修改该文件时,不会对其权限进行任何更改,但如果您正在制作另一个文件,请记住完全按照原样设置权限。

Finally, please do it on your own risk. It worked for me. (Samsung Discovery S730M)

最后,请自行承担风险。它对我有用。(三星探索 S730M)

回答by S.D.

When phone get charging from outlet, I want phone to power up automatically without hitting any power button. (Not Working with wall Socket, but working when connected to USB cable via Laptop).

当手机从插座充电时,我希望手机在不按任何电源按钮的情况下自动开机。(不适用于墙上插座,但在通过笔记本电脑连接到 USB 电缆时工作)。

You can only achieve this by modifying your phone's OS files. Basically there is boot script/binary at /system/bin/chargemonwhich you can replace with a script that does nothing. Do it at your own risk because this may result in the device being damaged permanently. Also, Manufacturer warranty will become void.

您只能通过修改手机的操作系统文件来实现这一点。基本上有引导脚本/二进制文件/system/bin/chargemon,您可以在其中替换为不执行任何操作的脚本。请自行承担风险,因为这可能会导致设备永久损坏。此外,制造商保修将失效。

回答by Boldbayar

i think there should be power sensor if you can add that in this code i belive it will work

我认为应该有功率传感器,如果您可以在此代码中添加它,我相信它会起作用

public class Main extends Activity {
        private SensorManager mSensorManager;
        private PowerManager mPowerManager;
        private WindowManager mWindowManager;
        private WakeLock mWakeLock;
        private Button button;
        private TextView textView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
try{
            // Get an instance of the SensorManager
            mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

            // Get an instance of the PowerManager
            mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);

            // Get an instance of the WindowManager
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            mWindowManager.getDefaultDisplay();

            // Create a bright wake lock
            mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
                    .getName());

            setContentView(R.layout.main);
            textView = (TextView)findViewById(R.id.textView1);
            button = (Button)findViewById(R.id.button1);
            button.setOnClickListener(mButtonStopListener);


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("onCreate", e.getMessage());
        }
} // END onCreate

        View.OnClickListener mButtonStopListener = new OnClickListener() {
            public void onClick(View v) {
                try {
                    mWakeLock.release();
                    textView.setText("mWakeLock.release()");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.e("onPause",e.getMessage());
                }

            }
        };

        @Override
        protected void onResume() {
            super.onResume();
            /*
             * when the activity is resumed, we acquire a wake-lock so that the
             * screen stays on, since the user will likely not be fiddling with the
             * screen or buttons.
             */

            try {
                mWakeLock.acquire();
                textView.setText("mWakeLock.acquire()");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("onResume", e.getMessage());
            }

        }

        @Override
        protected void onPause() {
            super.onPause();

            // and release our wake-lock
            try {
                mWakeLock.release();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("onPause",e.getMessage());
            }
        }
}

回答by Dave

So I was trying to achieve this with a 3rd gen Moto G. It has a charge_only_mode file as per Rohit's answer, but simply moving/renaming it did not make the phone reboot on charge. Instead it just sat there with a Motorola logo. I got the same result when replacing charge_only_mode with either of the scripts referenced here.

所以我试图用第三代 Moto G 来实现这一点。根据 Rohit 的回答,它有一个 Charge_only_mode 文件,但只是移动/重命名它并没有使手机在充电时重新启动。相反,它只是坐在那里,带有摩托罗拉的标志。用此处引用的任一脚本替换charge_only_mode 时,我得到了相同的结果。

I did get it to work, however. I copied /system/bin/reboot into /system/bin/charge_only_mode, and that did the trick.

然而,我确实让它工作了。我将 /system/bin/reboot 复制到 /system/bin/charge_only_mode 中,这样就成功了。