![](/img/trans.png)
[英]How to disable(turn off) android device wifi in appium - AndroidDriver programmatically while test scripts is running?
[英]How to programmatically turn on (Power-On) the android device at a certain time?
@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.