Search code examples
androidc++broadcastreceiverqtquick2android-wifi

How to register Android Broadcast Reciever in my QT application?


I want to register broadcastReceiver in my application. I am writing a program to print some string when wifi is closed or wifi network is shifted to another network.

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>

Java class:

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);

}

header file:

JNIEXPORT void JNICALL Java_org_qtproject_example_WifiReceiver_getinfo(JNIEnv * /*env*/,
                                          jobject /*this_obj*/, jstring ssid)
{

    qDebug()<<"ssid is"<<ssid;

}

My expectation is when I close my wifi manually or shift to another wifi manually at least I would get some result due to this line System.out.println("ashoish"); But when I am trying to do so, ashoish is not getting printed which shows onRecieve() is not executed.

Is their any problem in registering BroadcastReciever in manifest file? How can I solve this?


Solution

  • This problem is solved by programmatically registering BroadcastReciever..