一、直接在android中拦截
1.1 写一个java类MainActivity
package com.lsy.sharedata;
import java.util.Iterator;
import java.util.List;
import android.view.KeyEvent;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends org.qtproject.qt5.android.bindings.QtActivity {
private LocationManager lm;
public static final String TAG="TAG";
private static native void onExitApp(int exit);
//设置权限用的
private static final int BAIDU_READ_PHONE_STATE = 100;//定位权限请求
private static final int PRIVATE_CODE = 1315;//开启GPS权限
private static final String perms[] = {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 返回键移除二级悬浮窗
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
Log.e(TAG, "back.....");
onExitApp(0);
//return true;
}
return super.onKeyDown(keyCode, event);
}
}
1.2 在C++中注册回调:
void QtCallAndroid::RegJni()
{
#ifdef Q_OS_ANDROID
static JNINativeMethod methods[] {
{ "onGPSNotify", "(Ljava/lang/String;I)V", (void*)onGPSDataChanged },
{ "onExitApp", "(I)V", (void*)onAppExitChanged }
};
const char* kClassName ="com/lsy/sharedata/MainActivity";
QtAndroid::runOnAndroidThreadSync([=](){
QAndroidJniEnvironment qtEnv;
jclass clazz;
JNIEnv *EV = qtEnv.operator ->();
clazz =EV->FindClass(kClassName);
if (clazz == NULL)
{
qDebug()<<"erro clazz";
return ;
}
qDebug()<<"RegisterNatives 2***********";
if (EV->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) != JNI_OK)
{
printf("register native method failed!\n");
return;
}else{
qDebug()<<"RegisterNatives 搞定***********";
}
});
qDebug()<<"RegisterNatives OK***********";
#endif
}
3.实现C++中回调函数:
void QtCallAndroid::onAppExitChanged(JNIEnv *env, jobject thiz , jint exit)
{
qDebug()<<"java send app exit..................";
}
?运行结果如下:
<!--Android中先拦截到Back按键-->
E TAG : back.....
<!--注册到java中的回调次之-->
D libAcquireWealth_armeabi-v7a.so: DEBUG[ FILE: ..\..\AcquireWealth\qtcallandroid.cpp LINE:97 ] java send app exit..................
<!-- Qt事件过滤器再次之-->
D libAcquireWealth_armeabi-v7a.so: DEBUG[ FILE: ..\..\AcquireWealth\qdoublecolorballmanager.cpp LINE:854 ] back....................
<!-- 最后传回到QML-->
D libAcquireWealth_armeabi-v7a.so: qml: DEBUG[ FILE: qrc:/main.qml LINE:92 ] accepted now
D libAcquireWealth_armeabi-v7a.so: qml: DEBUG[ FILE: qrc:/main.qml LINE:93 ] 按下了返回按键.....
二、在Qt中拦截
2.1 写一个键盘事件过滤类 KeyPressFilter,主要重写eventFilter。
protected:
//事件过滤
bool eventFilter(QObject *watched, QEvent *event);
该函数具体实现:
bool KeyPressFilter::eventFilter(QObject *watched, QEvent *event)
{
//获取事件类型
if(event->type() == QEvent::KeyPress)
{
//转换成键盘事件
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
//判断是否是back事件
if(keyEvent->key() == Qt::Key_Back)
{
//发送back键按下的信号
emit sigKeyBackPress();
return true;
}
}
return false;//
}
2.2 在main.cpp中把KeyPressFilter注册到qml并添加过滤器
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("KeyPressFilter", KeyPressFilter::GetInstance());
QQmlComponent component(&engine, QUrl("qrc:/main.qml"));
//获取对象
QObject *object = component.create();
//添加过滤器
KeyPressFilter::GetInstance()->SetFilter(object);
2.3 在main.qml中捕捉
function onSigKeyBackPress(){
console.log("按下了返回按键.....")
}
效果如下图:
|