繁体   English   中英

如何在某个时间以编程方式打开(开机)android设备?

[英]How to programmatically turn on (Power-On) the android device at a certain time?

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

我知道这是可能的,因为我的手机(联想 A516)和其他一些手机具有此功能 --->>>截图<<<---

..需要一些想法开始..我可以为此使用闹钟吗?

@PM77-1 确实回答了您的问题。 在 AOSP 中,没有 API 可以在给定时间打开手机(从关闭状态)。 但是,有些手机确实支持此功能,但其实现取决于手机。 一般来说,没有办法从关机状态打开手机(不管root权限和系统权限如何)。 话虽如此,有一种方法可以重启手机。 但有趣的是,没有办法以编程方式关闭手机。 (OP有根,这不适用)

这是一个解决方案(在 Lenovo A516,API LEVEL 17 上测试,但对于具有 此处所述的“预定电源开/关功能”的其他设备,它应该可以正常工作):

卸载 /system/app/SchedulePowerOnOff.apk

编译并安装此代码

enableAlertPowerOn(...) 是神奇发生的函数:

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 必须是 "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>

示例如何从 Activity 调用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM