按Android平板(或手机)的物理按键(如音量键)实现Activity界面的跳转

一.需求分析:

1.通过按Android平板(或手机)的物理按键(如音量键)实现Activity界面的跳转;以前基本是自己写控件进行跳转啊!

2.按音量减键跳转:界面1到界面2,2到3,3到4,4到5,5到1;按音量加键跳转:1到5,5到4,4到3,3到2,2到1。      

五个界面颜色1:白色,2:黑色,3:红,4:绿,5:蓝。           目的是用来进行屏的测试。


二、直接上代码

1.五个类的代码

Launcher1:

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher1 extends Activity implements KeyEvent.Callback {
	private static final String TAG = "Launcher";
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 1.隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隐藏状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		// 3.隐藏导航栏和状态栏
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_one);
		
	}

	// 4. 拦截系统热键
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();//获取物理按键的key类型:比如音量键,power键等
		int key1 = event.getAction();//获取某一物理按键的对应的事件类型;比如音量键的按下(down)事件,音量键的松开(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {//按下的是安卓物理左键或者是音量键上键

			if (key1 == KeyEvent.ACTION_UP) {//音量键上键的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher1.this, Launcher5.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {//按下的是安卓物理右键或者是音量键下键

			if (key1 == KeyEvent.ACTION_UP) {//音量键下键的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher1.this, Launcher2.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}
}
Launcher2:
package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher2 extends Activity {
	private static final String TAG = "Launcher2";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隐藏状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隐藏导航栏和状态栏
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_two);

	}

	// 拦截系统热键
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 获取物理按键的key类型:比如音量键,power键等
		int key1 = event.getAction();// 获取某一物理按键的对应的事件类型;比如音量键的按下(down)事件,音量键的松开(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左键或者是音量键上键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键上键的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher2.this, Launcher1.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右键或者是音量键下键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键下键的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher2.this, Launcher3.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}
Launcher3:

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher3 extends Activity {
	private static final String TAG = "Launcher3";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隐藏状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隐藏导航栏和状态栏
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_three);

	}

	// 拦截系统热键
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 获取物理按键的key类型:比如音量键,power键等
		int key1 = event.getAction();// 获取某一物理按键的对应的事件类型;比如音量键的按下(down)事件,音量键的松开(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左键或者是音量键上键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键上键的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher3.this, Launcher2.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右键或者是音量键下键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键下键的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher3.this, Launcher4.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}


Launcher4:

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher4 extends Activity {
	private static final String TAG = "Launcher4";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隐藏状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隐藏导航栏和状态栏
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_four);
	}

	// 拦截系统热键
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 获取物理按键的key类型:比如音量键,power键等
		int key1 = event.getAction();// 获取某一物理按键的对应的事件类型;比如音量键的按下(down)事件,音量键的松开(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左键或者是音量键上键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键上键的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher4.this, Launcher3.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右键或者是音量键下键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键下键的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher4.this, Launcher5.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}


Launcher5:

package com.zhc.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Launcher5 extends Activity {
	private static final String TAG = "Launcher5";

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1.隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 2.隐藏状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// 3.隐藏导航栏和状态栏
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				| View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);

		setContentView(R.layout.activity_five);
	}

	// 拦截系统热键
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int key = event.getKeyCode();// 获取物理按键的key类型:比如音量键,power键等
		int key1 = event.getAction();// 获取某一物理按键的对应的事件类型;比如音量键的按下(down)事件,音量键的松开(up)事件
		if (key == KeyEvent.KEYCODE_DPAD_LEFT
				|| key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左键或者是音量键上键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键上键的up事件
				Log.i(TAG, "1");
				Intent intentright = new Intent(Launcher5.this, Launcher4.class);
				startActivity(intentright);

				return true;
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT
				|| key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右键或者是音量键下键

			if (key1 == KeyEvent.ACTION_UP) {// 音量键下键的up事件
				Log.i(TAG, "2");
				Intent intentright = new Intent(Launcher5.this, Launcher1.class);
				startActivity(intentright);

				return true;
			}
		}

		return super.dispatchKeyEvent(event);
	}

}

2.对应的五个Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="1" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >
 <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="2" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000" >
    
 <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="3" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FF00" >
     <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="4" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000FF" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="5" />

</RelativeLayout>

3.清单文件

  <activity android:name="com.zhc.launcher.Launcher2"/>
        <activity android:name="com.zhc.launcher.Launcher3"/>
        <activity android:name="com.zhc.launcher.Launcher4"/>
        <activity android:name="com.zhc.launcher.Launcher5"/>

三、gif图不知到弄,图片不贴了,说的很清楚了。


四、用到的相关知识点所参考的博文

1.KeyEvent类的相关知识点:
event.getKeyCode();// 获取物理按键的key类型:比如音量键,power键等。

event.getAction();// 获取某一物理按键的对应的事件类型;比如音量键的按下(down)事件,音量键的松开(up)事件

参考博文:http://blog.csdn.net/ccpat/article/details/45176665


2.应用拦截普通按键的处理;应用拦截热键的处理;

参考博文:http://www.open-open.com/lib/view/open1371203634703.html


3.隐藏标题栏+状态栏+导航栏

隐藏标题栏+状态栏 参考博文:1》http://www.cnblogs.com/tc310/p/4024686.html     2》http://blog.csdn.net/zyp009/article/details/16982253

隐藏导航栏+状态栏 参考博文:1》http://blog.csdn.net/myarrow/article/details/25606653    2》http://www.cnblogs.com/tc310/p/4024686.html (这个没实验成功)


猜你喜欢

转载自blog.csdn.net/ningchao328/article/details/52949464