实验环境:
Android5.1
不用系统platform签名,但root 了
代码
package com.hisense.sendbackkey;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Instrumentation;
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 sendBack() {
new Thread() {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
} catch (Exception e) {
}
}
}.start();
}
public void sendBack2(){
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
}catch (Exception e){
e.printStackTrace();
}
}
public void onBtnBack(View view){
Log.i(TAG, "点了BACK 请求返回键");
//sendBack();
sendBack2();
}
}
以上2个办法都可以实现返回键
有人説要加这个权限,实际意义不大。
<uses-permission android:name="android.permission.INJECT_EVENTS" />