如何(从本机代码)将android.content.Context参数发送到Java fct(通过JNI)
我正在为我的android手机开发一个应用程序,我正在尝试启用Wifi热点。 我使用Qt5.4.1,所以我在C++中开发。 由于NDK中没有任何函数可以执行此操作,因此我使用JNI调用Java方法 我的java代码是(感谢Ashish Sahu在中的回答): C++代码示例:如何(从本机代码)将android.content.Context参数发送到Java fct(通过JNI),java,android,c++,qt,java-native-interface,Java,Android,C++,Qt,Java Native Interface,我正在为我的android手机开发一个应用程序,我正在尝试启用Wifi热点。 我使用Qt5.4.1,所以我在C++中开发。 由于NDK中没有任何函数可以执行此操作,因此我使用JNI调用Java方法 我的java代码是(感谢Ashish Sahu在中的回答): C++代码示例: setWifiApEnabled(QAndroidJniObject context, bool b) { return QAndroidJniObject::callStaticMethod<jboolea
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);
}
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);
}
}
公共类AndroidMenusActivity扩展活动实现OnClickListener{
私人WifiApManager wifiMan;
私人开关按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
wifiMan=newwifipmanager((WifiManager)this.getSystemService(Context.WIFI_SERVICE));
setContentView(R.layout.activity\u android\u菜单);
makeUI();
}
私有void makeUI(){
线性布局子布局=(线性布局)findViewById(R.id.subLayout);
wifiButton=新的切换按钮(此按钮);
setexton(“禁用Wifi”);
wifiButton.setTextOff(“启用AP Wifi”);
wifiButton.setChecked(wifiMan.isWifiApEnabled());
setOnClickListener(this);
子布局。添加视图(wifiButton);
}
@凌驾
公共void onClick(查看发件人){
如果(!wifiButton.equals(发送方))
返回;
AsyncTask任务=新建AsyncTask(){
专用切换按钮bt;
私人WifiApManager wm;
@凌驾
受保护的布尔doInBackground(对象…args){
bt=(切换按钮)参数[0];
wm=(WifiApManager)参数[1];
返回wm.toggleWifi(“test.com”);
}
@凌驾
受保护的void onPostExecute(布尔结果){
bt.setChecked(result.booleanValue());
bt.setEnabled(真);
}
};
wifiButton.setEnabled(false);
执行(wifiButton,wifiMan);
}
}
<>但是在C++中,我找不到一个方法,任何帮助?< /p> < P>只是为了将上下文对象从C++传递到JNI,使用自动注入方式:
QtAndroid::androidActivity().callStaticMethod<void>(
"com/company/project/MyApp",
"myFunc", // method name
"(Landroid/content/Context;)V" // auto-injection
);
QtAndroid::androidActivity().callStaticMethod(
“com/company/project/MyApp”,
“myFunc”,//方法名称
“(Landroid/content/Context;)V”//auto-injection
);
我刚刚编辑了我的问题我成功地使用Java和Android Studio启用了Wifi热点,我编辑了我的问题以添加代码。有没有人知道如何用C++来做同样的事情?
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);
}
}
QtAndroid::androidActivity().callStaticMethod<void>(
"com/company/project/MyApp",
"myFunc", // method name
"(Landroid/content/Context;)V" // auto-injection
);