android PowerManager亮灭屏(实现篇)"/>
android PowerManager亮灭屏(实现篇)
网上查了好多资料 不是讲原理就是讲原理呀! 你Tm到是实现呀!!!墨迹一大篇文章,都是看的官文档然后加上自己的理解一顿墨迹。
具体讲下我要实现的功能:
Android 6.0实现自动亮灭屏幕,按下电源键屏幕灭屏5s后自动亮屏,时间可以自己设置
用到的知识有 1:powermanger的基础知识
2:超级管理员权限 重中之重呀!!!!
参考超级管理员链接点击打开链接
3:Handler的基础知识
4:BroadcastReceiver的广播通知的基础知识
5:基础知识相信大家找网上一大堆
6:基础链接我就不给大家发了 相信你们比我找的好
据说4.0以后为了安全,谷歌兄做了很多安全设施。power电池管理就很难搞了,整了一周费劲巴力的弄了出来
废话不多说了直接上代码。 放心代码一定要全部。
首先是MainActivity 的代码
package com.example.unruly_zhao.powermanager_wakelock;import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button startBtn = null;private Button stopBtn = null;private EditText timeEditText = null;private EditText delayEditText = null;private TextView textviewResult = null;private TextView textviewVersion = null;/*** 设备管理*/public DevicePolicyManager mDPM;/*** 次数*/int time;/*** 延迟时间*/int delay;/*** 计数*/int timeCount;/*** 中间数*/int timemid;/*** 组件*/ComponentName devAdmReceiver;protected String tag = getClass().getSimpleName();protected static final int REQUEST_ENABLE = 0;protected static final int GUI_DOING = 0;protected static final int GUI_FINISH = 1;private KeyguardManager km;private KeyguardLock kl;private PowerManager pm;private PowerManager.WakeLock wl;LockScreen lockScreen = new LockScreen();int sucCount;int failCount;String versionName = "2016_10_14_WakeUp_1.0";/*** handler deal with msg*/Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case GUI_DOING:Log.e(tag, "doing-->执行");String string = (String) msg.obj;textviewResult.setText(string);textviewResult.setGravity(Gravity.CENTER);break;case GUI_FINISH:Log.e(tag, "doing-->完成");String string1 = (String) msg.obj;textviewResult.setText(string1);textviewResult.setGravity(Gravity.CENTER);break;default:break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final IntentFilter filter = new IntentFilter();filter.addAction(Intent.ACTION_SCREEN_OFF);fi
更多推荐
android PowerManager亮灭屏(实现篇)
发布评论