如何在我的应用程序(系统应用程序+根设备)中的Android Studio(API级别>=17)中实现它?

我知道这是可能的,因为我的手机(联想A516)和其他一些手机都有这个功能

..首先需要一些 idea ..

推荐答案

下面是一个解决方案(在联想A516 API级别17上测试,但它应该适用于其他"Scheduled power On/Off feature"here的设备):

Uninstall /system/app/SchedulePowerOnOff.apk

编译并安装此代码

EnableAlertPower(…)是魔术发生的函数:

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);
}

发送接收器.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
    }
}

建筑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>

如何从活动中调用enableAlertPowerOn(Context context, long atTimeInMillis)的示例:

  @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();
}

Java相关问答推荐

为什么一个java函数会返回一个作为参数传递给它的对象?

springboot start loge change

条件加载@ManyToMany JPA

使用意向过滤器从另一个应用程序启动服务

如何在Spring Java中从数据库列中获取JSON中的具体数据

基于接口的投影、原生查询和枚举

使用While循环打印素数,无法正常工作

根本不显示JavaFX阿拉伯字母

Java 11 HttpCookie.parse在解析包含JSON的Cookie时引发IlLegalArgumentException

如何以编程方式保存workBench.xmi?

JavaFX复杂项目体系 struct

Cucumber java-maven-示例表-未定义一步

JOOQ:批处理CRUD操作使用动态表定义,如何?

升级版本后出现非法访问错误

使用StringBuilder和append方法创建字符串时Java字符串内部方法的问题

如何显示新布局

声纳覆盖范围为 0%,未生成 jacoco.xml

我可以使用一个 PoolingNHttpClientConnectionManager 运行多个 HttpAsyncClient 吗?

setMaxTotal实际上是做什么的? (MySQL与Java和Apache BasicDataSource连接池)

Lambda 表达式正确执行,但匿名类定义抛出错误