Android Studio 基礎 之 模擬 Home / 音量 + - 加減鍵按下(擴展到模擬常用的各個物理按鍵)的操作
目錄
Android Studio 基礎 之 模擬 Home / 音量 + - 加減鍵按下
一、簡單介紹
Android 開發中的一些基礎操作,使用整理,便於後期使用。
本節介紹,模擬 Home / 音量 + - 加減鍵按下的操作。
二、實現原理
1、音量加減+-鍵,使用 Runtime 和 Process (Process proc = runtime.exec(keyCommand))實現
2、Home 鍵,使用意圖 intent 的方式實現
3、模擬 adb shell input key keynumber,來實現各個物理按鍵
三、注意實現
1、要點擊實現音量真正的加減效果,請在音量調出現的時候,繼續按下音量加減,音量就會真正的增加減少
2、代碼模擬其他物理按鍵輸入,這裏用了shell 命令在程序運行時環境裏模擬按鍵事件。因命令執行會阻塞線程, 所以最好是在子線程裏執行命令
四、效果預覽
五、實現步驟
1、打開 Android Studio ,新建工程
2、取個名稱
3、新建一個 EmptyActivity
4、完成,新建工程
5、編寫腳本,實現模擬音量加減 / 和Home 鍵的功能
6、佈置一下 layout,測試接口
7、打包運行應用,效果如上
六、關鍵代碼
1、MainActivity.java
package com.example.administrator.simulatevolumekey;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onVolumeAddKeyclick(View view) {
// 模擬音量+
OnVolumeAddKey();
}
public void onVolumeReduceKeyclick(View view) {
// 模擬音量-
OnVolumeReduceKey();
}
public void onHomeKeyclick(View view) {
// 模擬 Home 鍵
OnHomeKey();
}
/**
* 模擬音量加減按下
*/
public void OnVolumeAddKey(){
try
{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_VOLUME_UP;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
Log.i(TAG, "OnVolumeAddKey: 音量加按下");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 模擬音量減鍵按下
*/
public void OnVolumeReduceKey(){
try
{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_VOLUME_DOWN;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
Log.i(TAG, "OnVolumeAddKey: 音量減按下");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 模擬Home 鍵
*/
public void OnHomeKey(){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//提示如果是服務裏調用,必須加入new task標識
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
}
2、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="84dp"
android:onClick="onVolumeAddKeyclick"
android:text="模擬音量+"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="176dp"
android:onClick="onVolumeReduceKeyclick"
android:text="模擬音量-"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="280dp"
android:onClick="onHomeKeyclick"
android:text="模擬Home鍵"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
七、擴展到模擬各個物理按鍵的代碼
package com.example.administrator.simulatevolumekey;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onVolumeAddKeyclick(View view) {
// 模擬音量+
//OnVolumeAddKey();
keyVolumeAdd();
}
public void onVolumeReduceKeyclick(View view) {
// 模擬音量-
OnVolumeReduceKey();
}
public void onHomeKeyclick(View view) {
// 模擬 Home 鍵
OnHomeKey();
}
/**
* 模擬音量加減按下
*/
public void OnVolumeAddKey(){
try
{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_VOLUME_UP;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
Log.i(TAG, "OnVolumeAddKey: 音量加按下");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 模擬音量減鍵按下
*/
public void OnVolumeReduceKey(){
try
{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_VOLUME_DOWN;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
Log.i(TAG, "OnVolumeAddKey: 音量減按下");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 模擬Home 鍵
*/
public void OnHomeKey(){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//提示如果是服務裏調用,必須加入new task標識
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
/**
* adb shell 命令 模擬音量鍵+
*/
public void keyVolumeAdd(){
new Thread(){
@Override
public void run() {
execByRuntime("input keyevent 24");
}
}.start();
}
/**
* 執行shell 命令, 命令中不必再帶 adb shell
*
* @param cmd
* @return Sting 命令執行在控制檯輸出的結果
*/
public static String execByRuntime(String cmd) {
Process process = null;
BufferedReader bufferedReader = null;
InputStreamReader inputStreamReader = null;
try {
process = Runtime.getRuntime().exec(cmd);
inputStreamReader = new InputStreamReader(process.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
int read;
char[] buffer = new char[4096];
StringBuilder output = new StringBuilder();
while ((read = bufferedReader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
return output.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (null != inputStreamReader) {
try {
inputStreamReader.close();
} catch (Throwable t) {
//
}
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (Throwable t) {
//
}
}
if (null != process) {
try {
process.destroy();
} catch (Throwable t) {
//
}
}
}
}
}
八、常用的按鍵keyCode
鍵英文名 | 鍵中文名 | Keycode |
KEYCODE_CALL | 撥號鍵 | 5 |
KEYCODE_ENDCALL | 掛機鍵 | 6 |
KEYCODE_HOME | 按鍵Home | 3 |
KEYCODE_MENU | 菜單鍵 | 82 |
KEYCODE_BACK | 返回鍵 | 4 |
KEYCODE_SEARCH | 搜索鍵 | 84 |
KEYCODE_CAMERA | 拍照鍵 | 27 |
KEYCODE_FOCUS | 拍照對焦鍵 | 80 |
KEYCODE_POWER | 電源鍵 | 26 |
KEYCODE_NOTIFICATION | 通知鍵 | 83 |
KEYCODE_MUTE | 話筒靜音鍵 | 91 |
KEYCODE_VOLUME_MUTE | 揚聲器靜音鍵 | 164 |
KEYCODE_VOLUME_UP | 音量增加鍵 | 24 |
KEYCODE_VOLUME_DOWN | 音量減小鍵 | 25 |
KEYCODE_ENTER | 回車鍵 | 66 |
KEYCODE_ESCAPE | ESC鍵 | 111 |
KEYCODE_DPAD_CENTER | 導航鍵 確定鍵 | 23 |
KEYCODE_DPAD_UP | 導航鍵 向上 | 19 |
KEYCODE_DPAD_DOWN | 導航鍵 向下 | 20 |
KEYCODE_DPAD_LEFT | 導航鍵 向左 | 21 |
KEYCODE_DPAD_RIGHT | 導航鍵 向右 | 22 |
KEYCODE_MOVE_HOME | 光標移動到開始鍵 | 122 |
KEYCODE_MOVE_END | 光標移動到末尾鍵 | 123 |
KEYCODE_PAGE_UP | 向上翻頁鍵 | 92 |
KEYCODE_PAGE_DOWN | 向下翻頁鍵 | 93 |
KEYCODE_DEL | 退格鍵 | 67 |
KEYCODE_FORWARD_DEL | 刪除鍵 | 112 |
KEYCODE_INSERT | 插入鍵 | 124 |
KEYCODE_TAB | Tab鍵 | 61 |
KEYCODE_NUM_LOCK | 小鍵盤鎖 | 143 |
KEYCODE_CAPS_LOCK | 大寫鎖定鍵 | 115 |
KEYCODE_BREAK | Break/Pause鍵 | 121 |
KEYCODE_SCROLL_LOCK | 滾動鎖定鍵 | 116 |
KEYCODE_ZOOM_IN | 放大鍵 | 168 |
KEYCODE_ZOOM_OUT | 縮小鍵 | 169 |