Android Studio 基礎 之 模擬 Home / 音量 + - 加減鍵按下
目錄
Android Studio 基礎 之 模擬 Home / 音量 + - 加減鍵按下
一、簡單介紹
Android 開發中的一些基礎操作,使用整理,便於後期使用。
本節介紹,模擬 Home / 音量 + - 加減鍵按下的操作。
二、實現原理
1、音量加減+-鍵,使用 Runtime 和 Process (Process proc = runtime.exec(keyCommand))實現
2、Home 鍵,使用意圖 intent 的方式實現
三、注意實現
1、要點擊實現音量真正的加減效果,請在音量調出現的時候,繼續按下音量加減,音量就會真正的增加減少
四、效果預覽
五、實現步驟
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>