1

for learning purposes, I am trying to write a simple sleep timer app. Purpose should be to have a picker, select a number of minutes and after that time, perform certain actions. Most importantly, activate flight mode and close all other applications to save the battery.

I have a custom picker and my runnable thread, which works fine:

@Override
public void run() {

    while((System.currentTimeMillis() - startTime ) / 1000 < minutes){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("interrupted, go again.");
            return;
        }
    }

    System.out.println("Wait time is over, go to sleep.");
    app.timeToSleep();

however, within "timeToSleep" I now want to do things like:

Device.setFlightmodeEnabled(true); or

TaskManager.killall();

How would I achieve something like that? I have not found anything so far, but maybe I have the wrong key words to look for.

Thanks and best regards

Lequi
  • 539
  • 2
  • 13

1 Answers1

1

You can access some device features via API and some via native interfaces but I don't think setting a device to flightmode is technically possible without operator or Google authorization and that's only possible on Android...

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thanks for that hint. "Native Interface" was the keyword that I was lacking during my search, so I got the going for me, which is nice. Now that I have my android code file in the android native subfolder, I assumed that I can use code directly as others would use it. So when looking for turning off Wifi, I found this article: [http://stackoverflow.com/questions/5674333/how-to-enable-disable-wifi-from-an-application]. However, I do have no Android manifest to set the permissions for this? Something else that I missed? I have not found anything in the CN1 settings either. – Lequi May 10 '17 at 14:59
  • See https://www.codenameone.com/manual/advanced-topics.html in native interfaces you need to create an interface that abstracts your interaction with the native layer. To add things to the Android manifest check out the build hints at the top of that article. You can search codenameone.com to find additional details https://www.codenameone.com/search.html – Shai Almog May 11 '17 at 05:48