To connect Android BroadcastReceiver onReceive() method with cpp
-
Hi guys
I want to connect public void onReceive(Context context, Intent intent) to my cpp file.
I tried to connect it in this way..QAndroidJniObject intent("android/content/Intent"); QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/WifiReceiver","onReceive","(Landroid/content/Context;Landroid/content/Intent;)V;",QtAndroid::androidContext().object(),intent.object());
but I am getting error
W System.err: java.lang.NoSuchMethodError: no static method "Lorg/qtproject/example/WifiReceiver;.onReceive(Landroid/content/Context;Landroid/content/Intent;)V;"
My manifest
<receiver android:name="org.qtproject.example.WifiReceiver" > <intent-filter android:priority="100"> <action android:name="android.net.wifi.STATE_CHANGE" /> </intent-filter> </receiver>
How can I solve this error?
-
The error indicates that onReceive() is not static method in the class. Did you check it ?
-
Hi sir @dheerendra
Actually I was doing it in wrong way..
Challenge in front of me is that I at least want to print a string when my wifi network is changed to another wifi network or else if I closed wifi.
My java file:
public class WifiReceiver extends BroadcastReceiver { String ssid; @Override public void onReceive(Context context, Intent intent) { NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if(info != null && info.isConnected()) { System.out.println("ashoish"); WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); //ssid = wifiInfo.getSSID(); ssid="ashish";//SOME STRING getinfo(ssid); } } public native static String getinfo(String ssid); }
My Manifest:
<receiver android:name="org.qtproject.example.WifiReceiver" > <intent-filter android:priority="100"> <action android:name="android.net.wifi.STATE_CHANGE" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.net.wifi.WIFI_STATE_CHANGED"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
In header:
JNIEXPORT void JNICALL Java_org_qtproject_example_WifiReceiver_getinfo(JNIEnv * /*env*/, jobject /*this_obj*/, jstring ssid) { qDebug()<<"ssid is"<<ssid; }
My minimum need is to get this statement executed System.out.println("ashoish"); when wifi network is shifted or if wifi Is closed. But it is not getting executed . I think I have not registered BroadcastReciever properly in my manifest file....
How can I solve this problem?
-
Hi Sir @dheerendra
I got the solution.
I registered broadcast receiver programmatically and its working.. -
This is cool. If you put the code snippet of what you did will help others well.
-
Created MyActivity.java and called registerBroadcastReciever() in my main.cpp.
import android.net.NetworkInfo.DetailedState; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.app.Activity; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.content.BroadcastReceiver; import android.util.Log; import org.qtproject.qt5.android.bindings.QtActivity; import android.net.ConnectivityManager; import android.content.IntentFilter; public class MyActivity extends QtActivity { public void registerBroadcastReceiver() { WifiReceiver wifires =new WifiReceiver(); System.out.println("ASHISH123"); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); registerReceiver(wifires, intentFilter); } }
Main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "wificlass.h" #include<QQmlContext> #include <QtAndroid> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); WIFICLASS w; QQmlApplicationEngine engine; QtAndroid::androidActivity().callMethod<void>("registerBroadcastReceiver"); engine.rootContext()->setContextProperty("Ash",&w); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
It solved my problem of registering BroadcastReceiver..