Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中中捕獲HOME事件

Android中中捕獲HOME事件

編輯:關於Android編程

Android中中捕獲HOME事件,雖然網上有很多例子。但我照著弄大多不成功。在此重新寫一個例子。   首先,創建一個接收廣播的類   HLHomeKeyEventBroadCastReceiver.java      
package com.ldci.djrotatemenuforandroid;  
  
import com.huluo.welcome.HLWelcomeActivity;  
  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.util.Log;  
  
public class HLHomeKeyEventBroadCastReceiver extends BroadcastReceiver{  
  
     static final String SYSTEM_REASON = "reason";    
        static final String SYSTEM_HOME_KEY = "homekey";//home key    
        static final String SYSTEM_RECENT_APPS = "recentapps";//long home key    
      
      
    @Override  
    public void onReceive(Context context, Intent intent) {  
          
          
        // TODO Auto-generated method stub  
         String action = intent.getAction();    
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {    
                String reason = intent.getStringExtra(SYSTEM_REASON);    
                if (reason != null) {    
                    if (reason.equals(SYSTEM_HOME_KEY)) {    
                        // home key處理點    
                          
                        //停止服務  
                        context.stopService(HLWelcomeActivity.m_intent_RotateMenuIntent);  
  
                        Log.i("vvvvvvvvvv", "mmmmmm");  
                          
                    } else if (reason.equals(SYSTEM_RECENT_APPS)) {    
                        // long home key處理點    
                    }    
                }    
            }    
    }  
      
  
}  

 

    在程序的入口類中添加如下代碼:  
HLHomeKeyEventBroadCastReceiver receiver = new HLHomeKeyEventBroadCastReceiver();  
          
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  
          
        registerReceiver(receiver, intentFilter);  

 

  至此點擊Home事件即可捕捉。  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved