編輯:關於Android編程
有一部分三星的手機關閉屏幕以後,剛打開屏幕,前台的activity就會resume狀態,因為我們使用cocos2d-x引擎,默認resume和activity的resume事件是一致的,我們在resume裡把關閉的聲音重新打開,所以導致,用戶點亮屏幕以後(尚未解鎖),就會聽到游戲內的聲音(如果在關閉屏幕時我們游戲處於前台)。
/** * screen狀態廣播接收者 */ private class ScreenBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_SCREEN_ON.equals(intent.getAction())){ mScreenStateListener.onScreenOn(); }else if(Intent.ACTION_SCREEN_OFF.equals(intent.getAction())){ mScreenStateListener.onScreenOff(); }else if(Intent.ACTION_USER_PRESENT.equals(intent.getAction())){ mScreenStateListener.onUserPresent(); } } }
/** * 停止screen狀態更新 */ public void stopScreenStateUpdate() { mContext.unregisterReceiver(mScreenReceiver); } /** * 啟動screen狀態廣播接收器 */ private void startScreenBroadcastReceiver() { 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 final static boolean isScreenLocked(Context c) { android.app.KeyguardManager mKeyguardManager = (KeyguardManager) c.getSystemService(c.KEYGUARD_SERVICE); return mKeyguardManager.inKeyguardRestrictedInputMode(); }
package com.example.myfirstapp; import java.lang.reflect.Method; import android.app.Activity; import android.app.KeyguardManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.PowerManager; import android.util.Log; /** * 監聽屏幕ON和OFF PRESENT狀態 * * @author * @2014 * */ public class ScreenObserver { private static String TAG = "ScreenObserver"; private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateListener mScreenStateListener; private static Method mReflectScreenState; public ScreenObserver(Context context) { mContext = context; mScreenReceiver = new ScreenBroadcastReceiver(); try { mReflectScreenState = PowerManager.class.getMethod("isScreenOn", new Class[] {}); } catch (Exception nsme) { Log.d(TAG, "API < 7," + nsme); } } /** * screen狀態廣播接收者 */ private class ScreenBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { mScreenStateListener.onScreenOn(); } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { mScreenStateListener.onScreenOff(); } else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) { mScreenStateListener.onUserPresent(); } } } /** * 請求screen狀態更新 */ public void requestScreenStateUpdate(ScreenStateListener listener) { mScreenStateListener = listener; startScreenBroadcastReceiver(); firstGetScreenState(); } /** * 第一次請求screen狀態 */ private void firstGetScreenState() { PowerManager manager = (PowerManager) mContext .getSystemService(Activity.POWER_SERVICE); if (isScreenOn(manager)) { if (mScreenStateListener != null) { mScreenStateListener.onScreenOn(); } } else { if (mScreenStateListener != null) { mScreenStateListener.onScreenOff(); } } } /** * 停止screen狀態更新 */ public void stopScreenStateUpdate() { mContext.unregisterReceiver(mScreenReceiver); } /** * 啟動screen狀態廣播接收器 */ private void startScreenBroadcastReceiver() { 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); } /** * screen是否打開狀態 */ private static boolean isScreenOn(PowerManager pm) { boolean screenState; try { screenState = (Boolean) mReflectScreenState.invoke(pm); } catch (Exception e) { screenState = false; } return screenState; } // 外部調用接口 public interface ScreenStateListener { public void onScreenOn(); public void onScreenOff(); public void onUserPresent(); } public final static boolean isScreenLocked(Context c) { android.app.KeyguardManager mKeyguardManager = (KeyguardManager) c .getSystemService(c.KEYGUARD_SERVICE); return mKeyguardManager.inKeyguardRestrictedInputMode(); } }
BroadcastReceiver除了接收用戶所發送的廣播消息之外,還有一個重要的用途:接收系統廣播。如果應用需要在系統特定時刻執行某些操作,就
android中使用jni對字符串加解密實現分析 最近項目有個需求,就是要對用戶的敏感信息進行加密處理,比如用戶的賬戶密碼,手機號等私密信息。在java中,就對字符串
話不多說先上圖:類似於這樣的效果,很簡單。這是一個listview,item是自定義的view,有兩個特點: 傳入長度,動態改變柱狀圖的長度;根據長度改變色值,
之前實現過一次這種效果的ExpandableListView:http://www.jb51.net/article/38482.htm,帶效果比較挫,最近,在參考聯系人