0

CyanogenMod/Lineage OS has a feature to turn of the screen by double tapping the status bar. It just turns off the screen, not locking the device. As far as I know, this is not possible with normal apps. The apps on the Store turn off the device by locking the device. I browsed the source code of Lineage OS, and if I understood it correctly, it seemed to use PowerManager.goToSleep().

I want to create an app to turn the screen off, for my personal use on a device that runs Lineage OS. Since it is Lineage OS, not a stock ROM, I can do more things such as getting the root access or flashing something.

But according to this question, that method is not accessible to third-party apps. I created a simple project and called that method, but when I tried to build the project, "error: cannot find symbol method goToSleep(int)" happened.

Can I create an app that calls that method using Android Studio and the official SDK? If not, how can I build the app that calls that method?

Added the code as requested:

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        pm.goToSleep(100);
    }
}

Manifest:

<manifest package="com.example.turnofftest"
      xmlns:android="http://schemas.android.com/apk/res/android">
    ....
    <uses-permission android:name="android.permissions.DEVICE_POWER" />
</manifest>

After reading the question "ApriOri" told, I knew I have to sign the app using the platform's certificate, which I can probably do, as Lineage OS is open-source. But first, I need to build it.

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

2 Answers2

0

You should make sure that your app has permissions to do that. Since it's a Linage OS ROM you can easily include your app as a system app that will have access to the PowerManager API.

The question you mentioned include some comments about the required permissions.

There is a specific question regarding the required permissions of PowerManager.goToSleep() here.

ApriOri
  • 2,618
  • 29
  • 48
  • I do not believe adding the permission would make the compilation error go away. Doesn't it cause a runtime error to lack permissions? I guess the method may be somehow hidden from the SDK. – Damn Vegetables Aug 08 '17 at 14:14
  • You're supposed to get PowerManager using getSystemService. and cast PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE); – ApriOri Aug 08 '17 at 14:22
0

Use this :

PowerManager pManager=(PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
try {
    pManager.getClass().getMethod("goToSleep", new Class[]{long.class}).invoke(pManager, SystemClock.uptimeMillis());
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
} catch (NoSuchMethodException e) {
    e.printStackTrace();
}

Manifest :

become system uid :

android:sharedUserId="android.uid.system"

add permission :

<uses-permission android:name="android.permission.DEVICE_POWER"/>
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Vic
  • 21
  • 2