簡體   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