Android 屏幕灭屏亮屏广播,屏幕灭屏亮屏监听,广播实现按键监听
生活随笔
收集整理的這篇文章主要介紹了
Android 屏幕灭屏亮屏广播,屏幕灭屏亮屏监听,广播实现按键监听
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
service 類注冊廣播進行監(jiān)聽
/*** 作者:created by meixi* 郵箱:13164716840@163.com* 日期:2018/9/27 09*/ public class Serview extends Service {/*** 廣播接受者*/private BroadcastReceiver mBatInfoReceiver;private String TAG = "lgq--------------------";@Overridepublic void onCreate() {super.onCreate(); // Log.d(TAG, "onCreate()"); // initNotification(); // initTodayData();initBroadcastReceiver(); // new Thread(new Runnable() { // public void run() { // startStepDetector(); // } // }).start(); // startTimeCount();}/*** 注冊廣播*/private void initBroadcastReceiver() {final IntentFilter filter = new IntentFilter();// 屏幕滅屏廣播filter.addAction(Intent.ACTION_SCREEN_OFF);//關(guān)機廣播filter.addAction(Intent.ACTION_SHUTDOWN);// 屏幕亮屏廣播filter.addAction(Intent.ACTION_SCREEN_ON);// 屏幕解鎖廣播 // filter.addAction(Intent.ACTION_USER_PRESENT);// 當長按電源鍵彈出“關(guān)機”對話或者鎖屏時系統(tǒng)會發(fā)出這個廣播// example:有時候會用到系統(tǒng)對話框,權(quán)限可能很高,會覆蓋在鎖屏界面或者“關(guān)機”對話框之上,// 所以監(jiān)聽這個廣播,當收到時就隱藏自己的對話,如點擊pad右下角部分彈出的對話框filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);//監(jiān)聽日期變化filter.addAction(Intent.ACTION_DATE_CHANGED);filter.addAction(Intent.ACTION_TIME_CHANGED);filter.addAction(Intent.ACTION_TIME_TICK);mBatInfoReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(final Context context, final Intent intent) {String action = intent.getAction();if (Intent.ACTION_SCREEN_ON.equals(action)) {Log.i(TAG, "screen on");} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {Log.i(TAG, "screen off");//改為60秒一存儲 // duration = 60000;} else if (Intent.ACTION_USER_PRESENT.equals(action)) {Log.i(TAG, "screen unlock"); // save();//改為30秒一存儲 // duration = 30000;} else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {Log.i(TAG, " receive Intent.ACTION_CLOSE_SYSTEM_DIALOGS");//保存一次 // save();} else if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {Log.i(TAG, " receive ACTION_SHUTDOWN"); // save();} else if (Intent.ACTION_DATE_CHANGED.equals(action)) {//日期變化步數(shù)重置為0 // Logger.d("重置步數(shù)" + StepDcretor.CURRENT_STEP); // save(); // isNewDay();} else if (Intent.ACTION_TIME_CHANGED.equals(action)) {//時間變化步數(shù)重置為0Log.i("lgq000000000000","sssss時間變化步數(shù)重置為0===="+action); // isCall(); // save(); // isNewDay();} else if (Intent.ACTION_TIME_TICK.equals(action)) {//日期變化步數(shù)重置為0Log.i("lgq0000000000000000","日期變化步數(shù)重置為0===="+action); // isCall(); // Logger.d("重置步數(shù)" + StepDcretor.CURRENT_STEP); // save(); // isNewDay();}}};registerReceiver(mBatInfoReceiver, filter);}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;} } <service android:name=".testt.Serview"><intent-filter><!-- 系統(tǒng)啟動完成后會調(diào)用--><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.intent.action.DATE_CHANGED" /><action android:name="android.intent.action.MEDIA_MOUNTED" /><action android:name="android.intent.action.USER_PRESENT" /><action android:name="android.intent.action.ACTION_TIME_TICK" /><action android:name="android.intent.action.ACTION_POWER_CONNECTED" /><action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /></intent-filter> </service> /*** 開啟計步服務(wù)*/private void setupService() {Intent intent = new Intent(this, Serview.class); // isBind = bindService(intent, conn, Context.BIND_AUTO_CREATE);startService(intent);}?
實現(xiàn)demo:安卓廣播監(jiān)聽按鍵事件和屏幕熄屏亮屏監(jiān)聽-Android文檔類資源-CSDN下載?
總結(jié)
以上是生活随笔為你收集整理的Android 屏幕灭屏亮屏广播,屏幕灭屏亮屏监听,广播实现按键监听的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 复变函数 —— 4. 什么是调和函数
- 下一篇: 内容提供者的概念