Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. To connect Android BroadcastReceiver onReceive() method with cpp

To connect Android BroadcastReceiver onReceive() method with cpp

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashajg
    wrote on 27 Nov 2018, 06:13 last edited by aha_1980
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 27 Nov 2018, 07:23 last edited by
      #2

      The error indicates that onReceive() is not static method in the class. Did you check it ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • A Offline
        A Offline
        ashajg
        wrote on 27 Nov 2018, 10:57 last edited by
        #3

        ok sir I ll correct it.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ashajg
          wrote on 28 Nov 2018, 06:06 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ashajg
            wrote on 29 Nov 2018, 06:37 last edited by
            #5

            Hi Sir @dheerendra

            I got the solution.
            I registered broadcast receiver programmatically and its working..

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 29 Nov 2018, 08:24 last edited by
              #6

              This is cool. If you put the code snippet of what you did will help others well.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ashajg
                wrote on 29 Nov 2018, 10:11 last edited by ashajg
                #7

                @dheerendra

                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..

                1 Reply Last reply
                0

                1/7

                27 Nov 2018, 06:13

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved