顯示具有 devThinking 標籤的文章。 顯示所有文章
顯示具有 devThinking 標籤的文章。 顯示所有文章

2011年10月29日 星期六

[Android] 螢幕保護

Want To

就像電腦的螢幕保護程式,當手機閒置一段時間,不會鎖螢幕或是螢幕暗下來

而是顯示我們想要的內容,例如輪播照片之類的


Try To

[Lock Screen/Keyguard]
  • 取代系統原有的 Lock Screen/Keyguard
    • May Relative API
      • DevicePolicyManager
      • KeyguardManager
      • WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
    • Issues:
      • What is the different between Lock Screen and Keyguard means?
      • Keyguard includes 螢幕鎖和圖案解鎖等。(refer. 1)
      • Many events should be considered, ex:
        • Screen off/on, Incoming call/SMS, Alert Message.....
      • 是否可以在原有的 Lock Screen/Keyguard 上再執行自己的 application?
  • Disable Lock Screen/Keyguard and using Service or Thread to record touch event. 如果事件間隔超過設定,則啟動 Activity。
    • Events handler problems.
    • Start Activity in Service is allowed?
// Using KeyguardManager can disable/re-enable keyguard
KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); 
KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock("Activity1"); 
mKeyguardLock.disableKeyguard(); 



[Live Wallpaper]
  • Live wallpaper still showing when screen is locked,所以若是僅顯示在螢幕鎖之下即可...
    • 動態設定 live wallpaper,也就是當螢幕鎖了,才設定(因為應該要不影響 user 原有的桌步設定),取消後恢復 user 原有設定。
    • 但沒有正式官方的相官 method。


* Reference
  1. android的KeyGuard_谢彦_新浪博客
  2. [Android]替换系统的Lock Screen - sunny09290的专栏 - 博客频道 - CSDN.NET
  3. Handling Screen OFF and Screen ON Intents

2011年10月20日 星期四

[Android] Is network available?

Error Message

Occurred on Desire API 2.2

當關閉行動網路時,透過以下的方法判斷是否有網路,卻回傳 true?!

public static boolean isNetworkAvailable(Context context) {  
    ConnectivityManager connectivity = (ConnectivityManager) context  
        .getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (connectivity == null) {  
        } else {  
            NetworkInfo[] info = connectivity.getAllNetworkInfo();  
            if (info != null) {  
                for (int i = 0; i < info.length; i++) {  
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
                        return true;  
                    }  
                }  
            }  
        }  
        return false;  
    }  

若使用飛安模式則是回傳 false,但此情況僅出現幾次, 之後則模擬不出來,所以是否有疑漏其他操作行為?


Solution

在 API 2.1, 2.2, 2.3.3 的手機上測以下三種模式下所對應的結果:

飛安模式
  • NetworkInfo: [Landroid.net.NetworkInfo;@xxxxx, 
  • activeInfo: null 
  • type: MOBILE, state: DISCONNECTED, detailed state: DISCONNECTED, isAvailable: false, isConnected: false 
  • 其他 type 的 values 皆為以下 
    • state: UNKNOWN, detailed state: IDLE, isAvailable: false, isConnected: false 


開啟行動網路 
  • NetworkInfo: [Landroid.net.NetworkInfo;@xxxx, 
  •  activeInfo: NetworkInfo: type: MOBILE[UMTS], state: CONNECTED/CONNECTED, reason: dataEnabled, extra: internet, roaming: false, failover: false, isAvailable: true 
  • type: MOBILE, state: CONNECTED, detailed state: CONNECTED, isAvailable: true 
  • 其他 type 的 values 則為以下 
    • state: UNKNOWN or DISCONNECTED, detailed state: IDLE/DISCONNECTED/CONNECTED, isAvailable: true(few types are true), isConnected: false(few types are true) 


關閉行動網路 
  • NetworkInfo: [Landroid.net.NetworkInfo;@444b0530, 
  • activeInfo: null 
  • type: MOBILE, state: DISCONNECTED, detailed state: DISCONNECTED, isAvailable: true, isConnected: false 
  • 其他 type 的 values 皆為以下
    • state: UNKNOWN/DISCONNECTED, detailed state: IDLE/DISCONNECTED, isAvailable: true(few types are false), isConnected: false 


所以 state 結果應該和我們的需求一致 ,因此猜測是否得到的是狀態正在轉換時的值,試著監聽網路狀態的改變,以下方式能得到和操作一致的結果,但不確定此種方式是否正確。
// briefly
TelephonyManager.listen(new PhoneStateListener() { 
    @Override  
    public void onDataConnectionStateChanged(int state) {  
        switch(state){  
            case TelephonyManager.DATA_DISCONNECTED:
                break;  
            case TelephonyManager.DATA_CONNECTING:
                break;  
            case TelephonyManager.DATA_CONNECTED: 
                break;  
        }  
    }                     
}, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);  

另一種則是 onReceiver action "android.net.conn.CONNECTIVITY_CHANGE" 
但在改變任何狀態時皆收不到相關的 action!!!?


Pending Issues
監聽網路狀態 Using receiver to listen action "android.net.conn.CONNECTIVITY_CHANGE"


* Reference
- Android – 判斷手機是否連上網路 (ConnectivityManager)
- network: android 网络判断 **
- android之ConnectivityManager简介,网络连接状态
- NetworkInfo - ConnectivityManager
- 网络连接状态的监听和判断(android,wifi,Gprs)
- Android判断网络状态方法详解
- 监听手机的网络连接状态