Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_OFF

因工作需要通过app设置android设备的休眠唤醒,有人说通过发送以下两个广播来实现:

android.intent.action.SCREEN_OFF

android.intent.action.SCREEN_OFF

代码实现如下:

Timer time = new Timer();
        time.schedule(new TimerTask() {
            @Override
            public void run()
            {
                if (status)
                {
                    Intent intent = new Intent();
                    intent.setAction(intent.ACTION_SCREEN_OFF);
                    MainActivity.this.sendBroadcast(intent);
                }
                else
                {
                    Intent intent = new Intent();
                    intent.setAction(intent.ACTION_SCREEN_ON);
                    MainActivity.this.sendBroadcast(intent);
                }

            }
        }, 1000, 1000);


编译运行,报如下错误:

Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_OFF from pid=6018, uid=10075

于是乎产生一个想法,如果将该app设置为系统app,是否能够正常运行呢?

1、将手机root;

2、将该app拷贝到/system/app目录下;

先拷贝到sd卡下,再用root explore工具将工具复制到/system/app

3、使用root explore设置app的权限为777,并且设置为GID,UID:

4、重启手机。


此时系统会自动安装该app,打开app,还是报无权限的错误,由此看来该广播只有系统才能发送。



你可能感兴趣的:(Android,基础知识)