編輯:關於android開發
1、首先定義 ScreenListener
package com.app.lib; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.PowerManager; /** * Created by ${zyj} on 2016/6/21. */ public class ScreenListener { private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateListener mScreenStateListener; public ScreenListener(Context context) { mContext = context; mScreenReceiver = new ScreenBroadcastReceiver(); } /** * screen狀態廣播接收者 */ private class ScreenBroadcastReceiver extends BroadcastReceiver { private String action = null; @Override public void onReceive(Context context, Intent intent) { action = intent.getAction(); if (Intent.ACTION_SCREEN_ON.equals(action)) { // 開屏 mScreenStateListener.onScreenOn(); } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 鎖屏 mScreenStateListener.onScreenOff(); } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解鎖 mScreenStateListener.onUserPresent(); } } } /** * 開始監聽screen狀態 * * @param listener */ public void begin(ScreenStateListener listener) { mScreenStateListener = listener; registerListener(); getScreenState(); } /** * 獲取screen狀態 */ private void getScreenState() { PowerManager manager = (PowerManager) mContext .getSystemService(Context.POWER_SERVICE); if (manager.isScreenOn()) { if (mScreenStateListener != null) { mScreenStateListener.onScreenOn(); } } else { if (mScreenStateListener != null) { mScreenStateListener.onScreenOff(); } } } /** * 停止screen狀態監聽 */ public void unregisterListener() { mContext.unregisterReceiver(mScreenReceiver); } /** * 啟動screen狀態廣播接收器 */ private void registerListener() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_USER_PRESENT); mContext.registerReceiver(mScreenReceiver, filter); } public interface ScreenStateListener {// 返回給調用者屏幕狀態信息 public void onScreenOn(); public void onScreenOff(); public void onUserPresent(); } }
2、使用
package com.app.lib; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ScreenListener screenListener ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); screenListener = new ScreenListener( MainActivity.this ) ; screenListener.begin(new ScreenListener.ScreenStateListener() { @Override public void onScreenOn() { Toast.makeText( MainActivity.this , "屏幕打開了" , Toast.LENGTH_SHORT ).show(); } @Override public void onScreenOff() { Toast.makeText( MainActivity.this , "屏幕關閉了" , Toast.LENGTH_SHORT ).show(); } @Override public void onUserPresent() { Toast.makeText( MainActivity.this , "解鎖了" , Toast.LENGTH_SHORT ).show(); } }); } }
lesson3-Qt對話框一、QDialog類1、對話框的概念對話框在各種軟件中都會使用到,一般用來給用戶提示信息或者接收用戶反饋的信息,因此對話框是應用程序和用戶交互的
在Android裡要實現樹形菜單,都是用ExpandableList(也有高手自己繼承ListVi
android的消息機制 很多以前掌握的知識,總是慢慢的再忘記,看來還是自己理解的不夠透徹,希望用博客的形式記錄下來。 說起android的消息機制,那不得不提Handl
AIDL 官網地址詳解分析: http://developer.android.com/guide/components/aidl.html 一)什麼是A