Android 开机向导界面,如果超过10分钟,自动关机 代码 demo

文章介绍了如何在Android系统的Settings模块中添加代码,以实现当开机向导界面超过10分钟后自动关机的功能。代码包括SimCompleteBootReceiver广播接收器和CountDownTimer的使用,以及检查用户设置是否完成的逻辑。
摘要由CSDN通过智能技术生成

Android 开机向导界面,如果超过10分钟,自动关机 代码 demo
在Settings 模块里面添加如下代码:
package com.android.settings.sim.receivers

git diff 如下

diff --git a/src/com/android/settings/sim/receivers/SimCompleteBootReceiver.java b/src/com/android/settings/sim/receivers/SimCompleteBootReceiver.java
index a7c9ecf..8777339 100644
--- a/src/com/android/settings/sim/receivers/SimCompleteBootReceiver.java
+++ b/src/com/android/settings/sim/receivers/SimCompleteBootReceiver.java
@@ -19,12 +19,15 @@ package com.android.settings.sim.receivers;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.os.CountDownTimer;
 import android.util.Log;
 
 import com.android.settings.hnlens.display.TouchSensitivityPreferenceController;
 import com.android.settings.sim.SimActivationNotifier;
 import com.android.settings.sim.SimNotificationService;
 
+import com.google.android.setupcompat.util.WizardManagerHelper;
+
 /** This class manage all SIM operations after device boot up. */
 public class SimCompleteBootReceiver extends BroadcastReceiver {
     private static final String TAG = "SimCompleteBootReceiver";
@@ -39,8 +42,50 @@ public class SimCompleteBootReceiver extends BroadcastReceiver {
             SimNotificationService.scheduleSimNotification(
                     context, SimActivationNotifier.NotificationType.NETWORK_CONFIG);
         }

+
+        //check setupwarized
+        if (WizardManagerHelper.isUserSetupComplete(context)){
+            Log.i(TAG, "onReceive: WizardManagerHelper isUserSetupComplete");
+        }else {
+            Log.i(TAG, "onReceive: WizardManagerHelper not com");
+            startCountdown(context);
+        }
     }
+
+    private CountDownTimer countDownTimer;
+    private final long totalTime = 5 * 60 * 1000; // 10 minutes in milliseconds
+    private void startCountdown(final  Context context) {
+        countDownTimer = new CountDownTimer(totalTime, 1000*60) {
+            @Override
+            public void onTick(long millisUntilFinished) {
+                long seconds = millisUntilFinished / 1000;
+                Log.i(TAG, "onReceive: countDownTimer ticker "+seconds);
+                //tvCountdown.setText(String.format(Locale.getDefault(), "%02d:%02d", seconds / 60, seconds % 60));
+                if (WizardManagerHelper.isUserSetupComplete(context)){
+                    countDownTimer.cancel();
+                    Log.i(TAG, "onReceive: countDownTimer cancel ");
+                }
+            }
+
+            @Override
+            public void onFinish() {
+                if (!WizardManagerHelper.isUserSetupComplete(context)){
+                    shutdownDevice(context);
+                }
+            }
+        }.start();
+    }
+
+    private void shutdownDevice(final  Context context) {
+        String ACTION_REQUEST_SHUTDOWN = "com.android.internal.intent.action.REQUEST_SHUTDOWN";
+        Intent shutdownIntent = new Intent(ACTION_REQUEST_SHUTDOWN);
+        shutdownIntent.putExtra("android.intent.extra.KEY_CONFIRM", false);
+        shutdownIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        context.startActivity(shutdownIntent);
+    }
+
 }

在packages/apps/Settings
patch -p0 < xxx.diff

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值