欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 定时熄屏

程序员文章站 2022-06-22 08:54:27
安卓机实现 一段时间无操作自动熄屏具体功能已实现,主要代码如下,欢迎留言分享~~~1.UI show_screen_layout.xml

安卓机实现 一段时间无操作自动熄屏

具体功能已实现,主要代码如下,欢迎留言分享~~~

1.UI show_screen_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/id_set_layout"
    android:background="@color/bg_test"
    android:orientation="vertical">
    <include layout="@layout/status_bar"/>
    <include layout="@layout/title_bar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp">

                <RadioGroup
                    android:id="@+id/radiogroup1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_x="3px"
                    >

                        <!--无操作10分钟后休眠.-->
                        <RadioButton
                            android:id="@+id/id_screen_rest_box"
                            android:layout_width="match_parent"
                            android:layout_height="60dp"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:layout_marginRight="10dp"
                            android:background="@color/white"
                            android:text="@string/auto_rest_str"
                            android:textSize="23sp"
                             />

                        <!--永不休眠.-->
                        <RadioButton
                            android:id="@+id/id_no_sleep_box"
                            android:layout_width="match_parent"
                            android:layout_height="60dp"
                            android:background="@color/white"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:layout_marginRight="10dp"
                            android:text="@string/no_sleep_str"
                            android:textSize="23sp"
                            />

                 </RadioGroup>
            </RelativeLayout>
         </LinearLayout>
    </ScrollView>
</LinearLayout>

2.AndroidManifest.xml 添加广播
设备管理器勾选app,激活

  <!-- 设备管理 -->
        <receiver android:name=".receiver.AdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data android:name="android.app.device_admin"
                android:resource="@xml/lock_screen" />
            <intent-filter>
                <action
                    android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

 <uses-permission android:name="android.permission.WAKE_LOCK"/>

  1. AdminReceiver
public class AdminReceiver extends DeviceAdminReceiver {
    @Override
    public DevicePolicyManager getManager(Context context) {
        return super.getManager(context);
    }
    @Override
    public ComponentName getWho(Context context) {
        return super.getWho(context);
    }

    /**
     * 禁用
     */
    @Override
    public void onDisabled(Context context, Intent intent) {
      //  Toast.makeText(context, "禁用设备管理", Toast.LENGTH_SHORT).show();
        super.onDisabled(context, intent);
    }
    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        return super.onDisableRequested(context, intent);
    }

    /**
     * 激活
     */
    @Override
    public void onEnabled(Context context, Intent intent) {
      //  Toast.makeText(context, "启动设备管理", Toast.LENGTH_SHORT).show();
        super.onEnabled(context, intent);
    }
    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
    }
    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
    }
    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
    @Override
    public IBinder peekService(Context myContext, Intent service) {
        return super.peekService(myContext, service);
    }

}

4.动态注册监控屏幕的广播,可以在onCreate()里添加

设备管理服务定义

  private DevicePolicyManager policyManager;
    private ComponentName componentName;


    public DevicePolicyManager getDevicePolicyManager() {
        return policyManager;
    }
    public ComponentName getComponentName() {
        return componentName;
    }
       //获取设备管理服务 
        policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        //AdminReceiver 继承自 DeviceAdminReceiver
        componentName = new ComponentName(this,AdminReceiver.class);
 	
 	   //动态注册监控屏幕的广播
    	IntentFilter mintentFilter = new IntentFilter();
        mintentFilter.addAction("android.intent.action.SCREEN_ON");
        mintentFilter.addAction("android.intent.action.SCREEN_OFF");
        ScreenReceiver mscreenReceiver = new ScreenReceiver();
        registerReceiver(mscreenReceiver,mintentFilter);

5.ScreenReceiver 熄屏亮屏监控

public class ScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.SCREEN_ON")) { //亮屏
            if(MyApp.getInstance().isScreen()){
                MyApp.getInstance().startAD();//计时
            }
        }
        if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) { // 熄屏
            if(MyApp.getInstance().isScreen()){
                MyApp.getInstance().handler1.removeCallbacks(MyApp.getInstance().runnable1);//取消计时
            }
        }
    }

6.操作计时

//重写onTouch 方法
public class myOnTouchListener implements View.OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event){
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
               // 触摸
                if(MyApp.getInstance().isScreen()){
                       MyApp.getInstance().handler1.removeCallbacks(MyApp.getInstance().runnable1);//取消计时
                }

                break;
            case MotionEvent.ACTION_UP:
              // 抬起
                if(MyApp.getInstance().isScreen()){
                    MyApp.getInstance().startAD();
                }

                break;
        }
        return true;
    }
}

7. 
   public Handler handler1 = new Handler();
   private long time1=1000*60*10;//10分钟
 public void startAD() {
        handler1.removeCallbacks(runnable1);
        handler1.postDelayed(runnable1, time1);
    }

    public Runnable runnable1 = new Runnable() {
        @Override
        public void run() {
            //   Log.d("MainActivity", "~~10分钟后无操作就熄屏~~");
            lock();
        }
    };

8.可手动激活设备管理器权限   设置--安全--勾选app--激活

    private void   lock() {
        boolean active = 	MyApp.getInstance().getDevicePolicyManager().isAdminActive(MyApp.getInstance().getComponentName());
        if (!active) { // 若无权限
            Log.d("lock", "~~没有权限~~");
            //  activeManage(); // 去获得权限
            MyApp.getInstance().getDevicePolicyManager().lockNow(); // 并锁屏
        } else {
            Log.d("lock", "~~已经有权限~~");
            MyApp.getInstance().getDevicePolicyManager().lockNow(); // 直接锁屏
        }
    }

9.调用

public void wifiClick() {
        if(MyApp.getInstance().isScreen()){
            MyApp.getInstance().handler1.removeCallbacks(MyApp.getInstance().runnable1);//取消计时
            MyApp.getInstance().startAD();//计时
        }
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        startActivity(intent);
    }

本文地址:https://blog.csdn.net/weixin_46052306/article/details/107157062