2

How to implement it in Android Studio(API LEVEL >= 17) in my App(System-App + rooted device)?

I know it is possible because my phone(Lenovo A516) and some others have this feature --->>>Screenshot<<<---

..need some ideas to start with.. Can i use AlarmClock for this?

Evgeny
  • 31
  • 1
  • 5
  • 1
    Does this answer your question? [Schedule Turn on/off android device programmatically on specific times](https://stackoverflow.com/questions/64884965/schedule-turn-on-off-android-device-programmatically-on-specific-times) – PM 77-1 Jun 29 '22 at 15:29
  • No, there is no solution. – Evgeny Jun 29 '22 at 15:44

2 Answers2

1

@PM77-1 did answer your question. In AOSP, there is no API that will turn on the phone (from an off state) at a given time. However, some phones do support this functionality but its implementation is dependent on the phone. In general, there is no way to turn a phone on from the off state (regardless of root privileges and system permission). That being said, there is a way to reboot a phone. but funnily enough there is no way to programmatically power a phone down. (OP has root, this doesn't apply)

  • In general? My question wasn't how to implement it on ALL phones, i want to implement it on my phone(Lenovo A516), that supports this feature. Read this: https://nextstart.online/2017/03/09/android_autopower_on_off and you will see there is a way to implement a system-app that turns a phone on(scheduled power on) from the off state at specified time. You can also schedule power off. – Evgeny Jun 29 '22 at 22:48
  • @Evgeny If you want a solution for a specific phone then edit your question and give the details about the phone you have. As it stands, your question reads "How to programmatically turn on (Power-On) the android device at a certain time?". This asks about a general Android phone, not whatever your specific phone is. –  Jun 29 '22 at 22:56
  • @Evgeny Also, if the link you provided is about your specific phone, it gives you the answer. "... now we know number 7 for auto power on, number 8 for deskcolck in Power off status." Referring to the argument `type` you pass into `setExact(type, triggerAtMillisTime, operation)` –  Jun 29 '22 at 22:58
  • You are right, a phone model was not specified(edited). – Evgeny Jun 29 '22 at 23:15
  • But still i wish i had this link and a working example as answer to my question. – Evgeny Jun 29 '22 at 23:27
  • 1
    @Evgeny Again, if the link is for your phone, then the following should work: `setExact(7, System.currentTimeMillis() + 1000*60*5, operation)`. This (in theory) should turn the device on (if it is off) in 5 minutes. –  Jun 30 '22 at 00:09
  • 1
    I have to use set(...), instead of setExact(), because setExact() is for API LEVEL >= 19. But it WORKS! – Evgeny Jul 03 '22 at 16:35
1

Here is a solution(tested on Lenovo A516, API LEVEL 17, but it should work similar for other devices with "Scheduled power On/Off feature" like described here):

Uninstall /system/app/SchedulePowerOnOff.apk

Compile and install this code

enableAlertPowerOn(...) is the function where the magic happens:

private static void enableAlertPowerOn(Context context, long atTimeInMillis){
   AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(context, SchPwrOnReceiver.class);
   PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
   am.set(7, atTimeInMillis, sender);
}

SchPwrOnReceiver.java:

package com.mediatek.schpwronoff.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SchPwrOnReceiver extends BroadcastReceiver {
    private static final int STALE_WINDOW = 1800;
    private static final String TAG = "SchPwrOnReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO optional
    }
}

build.gradle(:app), applicationId has to be "com.mediatek.schpwronoff":

apply plugin: 'com.android.application'

android {
  compileSdkVersion 29
  defaultConfig {
    applicationId "com.mediatek.schpwronoff"
    minSdkVersion 17
    targetSdkVersion 29
    versionCode 17
    versionName "4.2.2"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  implementation 'com.android.support:support-compat:28.0.0'
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="17" android:versionName="4.2.2" package="com.mediatek.schpwronoff">   

<application
    android:name=".MYApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
       android:launchMode="singleTop">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />

         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
    </activity>
           <receiver android:name=".receivers.SchPwrOnReceiver">
             <intent-filter>
                 <action android:name="com.android.settings.schpwronoff.PWR_ON_ALERT"/>
             </intent-filter>
          </receiver>      
  </application>

</manifest>

Example how to call enableAlertPowerOn(Context context, long atTimeInMillis) from Activity:

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        showDateTimePickerPowerOn(this);
  }



public void showDateTimePickerPowerOn(Context context) {
          final Calendar currentDate = Calendar.getInstance();
          date = Calendar.getInstance();
          new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                  date.set(year, monthOfYear, dayOfMonth);
                  new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                      @Override
                      public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                          date.set(Calendar.HOUR_OF_DAY, hourOfDay);
                          date.set(Calendar.MINUTE, minute);
                          date.set(Calendar.SECOND, 0);                             

                          long cTimeInMillis = date.getTimeInMillis();
                          enableAlertPowerOn(context, cTimeInMillis);
                      }
                  }, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), true).show();
              }
          }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
Evgeny
  • 31
  • 1
  • 5