android TV開發屏蔽home鍵,返回鍵

Android TV屏蔽返回鍵

重寫OnkeyDown方法

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(KeyEvent.KEYCODE_BACK==keyCode){
        return false;
    }
    return super.onKeyDown(keyCode, event);
}
home鍵比較特殊,是受系統保護的,無法通過常規手段屏蔽,找到一個靠譜的方法是,通過工具類監聽到home鍵的廣播,然後做處理,不過效果還不是很理想

一,創建工具類

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Home按鍵監聽類
 * 使用說明:
 * 1、初始化HomeListen
 * HomeListen homeListen = new HomeListen( this );
 * homeListen.setOnHomeBtnPressListener( new setOnHomeBtnPressListener(){
 *        @Override
 *        public void onHomeBtnPress( ){
 *           // 按下Home按鍵回調
 *        }
 *
 *        @Override
 *        public void onHomeBtnLongPress( ){
 *           // 長按Home按鍵回調
 *        }
 * });
 *
 * 2、在onResume方法中啓動HomeListen廣播:
 * homeListen.start();
 *
 * 3、在onPause方法中停止HomeListen廣播:
 * homeListen.stop( );
 * */
public class HomeListen {
    public HomeListen(Context context) {
        mContext = context;
        mHomeBtnReceiver = new HomeBtnReceiver( );
        mHomeBtnIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    public void setOnHomeBtnPressListener( OnHomeBtnPressLitener onHomeBtnPressListener ){
        mOnHomeBtnPressListener = onHomeBtnPressListener;
    }

    public void start( ){
        mContext.registerReceiver( mHomeBtnReceiver, mHomeBtnIntentFilter );
    }

    public void stop( ){
        mContext.unregisterReceiver( mHomeBtnReceiver );
    }

    class HomeBtnReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            receive( context, intent );
        }
    }

    private void receive(Context context, Intent intent){
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra( "reason" );
            if (reason != null) {
                if( null != mOnHomeBtnPressListener ){
                    if( reason.equals( "homekey" ) ){
                        // 按Home按鍵
                        mOnHomeBtnPressListener.onHomeBtnPress( );
                    }else if( reason.equals( "recentapps" ) ){
                        // 長按Home按鍵
                        mOnHomeBtnPressListener.onHomeBtnLongPress( );
                    }
                }
            }
        }
    }

    public interface OnHomeBtnPressLitener{
        public void onHomeBtnPress( );
        public void onHomeBtnLongPress( );
    }

    private Context mContext = null;
    private IntentFilter mHomeBtnIntentFilter = null;
    private OnHomeBtnPressLitener mOnHomeBtnPressListener = null;
    private HomeBtnReceiver mHomeBtnReceiver = null;
}


二、在activity調用

public class HomeListenActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_listen_layout);
        initHomeListen( );
    }

@Override
protected void onResume( ) {
    super.onResume();
    mHomeListen.start( );
}

    @Override
    protected void onPause() {
        super.onPause();
        mHomeListen.stop( );
    }

    private void initHomeListen( ){
        mHomeListen = new HomeListen( this );
        mHomeListen.setOnHomeBtnPressListener( new HomeListen.OnHomeBtnPressLitener( ) {
            @Override
            public void onHomeBtnPress() {
                Log.e("-----home鍵出現了----","-----home鍵出現了----");
                startSelfFromPendingIntent();
            }

            @Override
            public void onHomeBtnLongPress() {
                Log.e("-----home鍵出現了----","-----home鍵出現了----");
                startSelfFromPendingIntent();
            }
        });
    }

    private void showToast( String toastInfoStr ){
        Toast.makeText( this, toastInfoStr, Toast.LENGTH_LONG ).show( );
    }

    private HomeListen mHomeListen = null;

    private void startSelfFromPendingIntent() {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(MainActivity.this, MainActivity.class));

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
        try {
            pendingIntent.send();
        } catch (Exception e) {
            Log.e("", "stayTop fail");
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章