android 开发屏蔽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");
        }
    }

你可能感兴趣的:(android,开发,android,开发)