gpt4 book ai didi

java - 如何将 android.content.Context 参数(从 native 代码)发送到 Java fct(通过 JNI)

转载 作者:行者123 更新时间:2023-12-04 22:49:47 25 4
gpt4 key购买 nike

我正在为我的 Android 手机开发一个应用程序,我正在尝试启用 Wifi 热点。我正在使用 Qt 5.4.1,所以我用 C++ 开发。由于在 NDK 中没有任何函数可以执行此操作,因此我使用 JNI 来调用 Java 方法。

我的 java 代码是(感谢 Ashish Sahu 在 stackoverflow thread 中的回答):

package org.app.test;

import android.content.*;
import android.net.wifi.*;
import java.lang.reflect.*;

//Class that handles Wifi Hotspot (access point) configuration
public class ApManager {

//Is Wifi hotspot on or off ?
public static boolean isApOn(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
try {
Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifimanager);
}
catch (Throwable ignored) {}
return false;
}

//Turn Wifi hotspot on or off
public static boolean configApState(Context context, boolean b) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiConfiguration wificonfiguration = null;
try {
//if Wifi is on, turn it off
if(isApOn(context)) {
wifimanager.setWifiEnabled(false);
}
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wificonfiguration, b);
return true;
}
catch(Exception e) {
e.printStackTrace();
}
return false;
}
}

C++ 代码示例:

setWifiApEnabled(QAndroidJniObject context, bool b)
{
return QAndroidJniObject::callStaticMethod<jboolean>("org/app/test/ApManager"
, "configApState"
, "(Ljava/lang/Object;Z)Z" //Or (Landroid/content/Context;Z)Z ???
, context.object<jobject>()
, b);
}

但是现在我遇到了一个问题;如何获取参数 context 在我调用它时传递给函数 setWifiApEnabled(context, b)

我有点迷茫,我读了一些关于这个问题的帖子 (like this one),但我不完全理解回答者的意思。

你能帮我解决这个问题吗?

编辑:我在这个 stackoverflow thread 上找到了一种获取上下文的方法:

interface = QApplication::platformNativeInterface();
activiti = (jobject)interface->nativeResourceForIntegration("QtActivity");
at = new QAndroidJniObject(activiti);
appctx = at->callObjectMethod("getApplicationContext", "()Landroid/content/Context;");
if(appctx.isValid()) qDebug() << "I am valid !";
else qDebug() << "I ain't valid !";

appctx 有效,但 Wifi 热点未启用,我无法获取其状态。

编辑 2::我成功地使用 Android Studio 在 Java 中启用了 Wifi 热点。代码如下:

WifiApManager.java :

public class WifiApManager {
private WifiManager wifiMan;
protected Method setWifiApEnabledMethod, isWifiApEnabledMethod;
protected final static int MAX_ITER = 10;

public WifiApManager(WifiManager wifiMan) {
this.wifiMan = wifiMan;
getHiddenMethods();
}

private void getHiddenMethods() {
try {
setWifiApEnabledMethod = wifiMan.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
isWifiApEnabledMethod = wifiMan.getClass().getMethod("isWifiApEnabled");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

public boolean isWifiApEnabled() {
try {
return (Boolean)isWifiApEnabledMethod.invoke(wifiMan);
} catch (Exception e) {
return false;
}
}

public boolean isWifiEnabled() {
return wifiMan.isWifiEnabled();
}

public boolean setWifiApEnabled(WifiConfiguration conf, boolean enabled) {
try {
return (Boolean) setWifiApEnabledMethod.invoke(wifiMan, conf, true);
} catch (Exception e) {
return false;
}
}

public boolean toggleWifi(String ssid) {
// WifiConfiguration creation:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = ssid;
conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

// If AP Wifi is enabled, disables it and returns:
if(isWifiApEnabled()) {
//setWifiApEnabled(null, false); Won't work, see two further lines
wifiMan.setWifiEnabled(true);
wifiMan.setWifiEnabled(false);
int maxIter = MAX_ITER;
while (isWifiApEnabled() && maxIter-- >= 0) {
try {Thread.sleep(500);} catch (Exception e) {}
}
return isWifiApEnabled();
}

// If standard Wifi is enabled, disables it:
if (isWifiEnabled()) {
if (wifiMan.setWifiEnabled(false)) {
int maxIter = MAX_ITER;
while (wifiMan.isWifiEnabled() && maxIter-- >= 0) {
try {Thread.sleep(500);} catch (Exception e) {}
}
}
if (isWifiEnabled()) {
return false;
}
}

// Enables AP Wifi
try {
if (! setWifiApEnabled(conf, true)) {
System.out.println("setWifiApEnabledMethod failed.");
return false;
}
int maxIter = MAX_ITER;
while (! isWifiApEnabled() && maxIter-- > 0) {
try {Thread.sleep(500);} catch (Exception e) {}
}
} catch(Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}

主类:

public class AndroidMenusActivity extends Activity implements OnClickListener {
private WifiApManager wifiMan;
private ToggleButton wifiButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wifiMan = new WifiApManager((WifiManager) this.getSystemService(Context.WIFI_SERVICE));
setContentView(R.layout.activity_android_menus);
makeUI();
}

private void makeUI() {
LinearLayout subLayout = (LinearLayout) findViewById(R.id.subLayout);
wifiButton = new ToggleButton(this);
wifiButton.setTextOn("Disable Wifi");
wifiButton.setTextOff("Enable AP Wifi");
wifiButton.setChecked(wifiMan.isWifiApEnabled());
wifiButton.setOnClickListener(this);
subLayout.addView(wifiButton);
}

@Override
public void onClick(View sender) {
if (!wifiButton.equals(sender))
return;

AsyncTask<Object, Void, Boolean> task = new AsyncTask<Object, Void, Boolean>() {
private ToggleButton bt;
private WifiApManager wm;

@Override
protected Boolean doInBackground(Object... args) {
bt = (ToggleButton) args[0];
wm = (WifiApManager) args[1];
return wm.toggleWifi("test.com");
}
@Override
protected void onPostExecute (Boolean result) {
bt.setChecked(result.booleanValue());
bt.setEnabled(true);
}
};
wifiButton.setEnabled(false);
task.execute(wifiButton, wifiMan);
}
}

但是我找不到在 C++ 中做同样事情的方法,有什么帮助吗?

最佳答案

只是为了将上下文对象从 C++ 传输到 JNI,使用这样的自动注入(inject):

QtAndroid::androidActivity().callStaticMethod<void>(
"com/company/project/MyApp",
"myFunc", // method name
"(Landroid/content/Context;)V" // auto-injection
);

关于java - 如何将 android.content.Context 参数(从 native 代码)发送到 Java fct(通过 JNI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30762826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com