Receiving Broadcasts

As the QPair service broadcasts its status, applications can register a broadcast receiver to monitor the status. If you do not want to be confused by other services that send intents with the same action names as QPair’s, it is highly recommended that you set a permission for the broadcast receiver. Please see Registering a BroadcastReceiver for detailed implementation.

 

The QPair service broadcasts the following events.

 

Action String

Description

“com.lge.qpair.CALL_FOR_PROPERTIES”

Provides notification that QPair was initialized.

This does not mean the QPair connection has been established.

“com.lge.qpair.STATE_CHANGED”

Provides notification that QPair is on/off.

The extra field “com.lge.qpair.is_on” states whether QPair is on or off: true if it is on, false if it is off.

“com.lge.qpair.CONNECTION_CHANGED”

Provides notification of the QPair connection status.

The extra field“com.lge.qpair.connected” states whether the connection is established or not: true if it is established, false if it is not established.

 

All of the action strings and extra field strings are defined in the QPairConstants class.
Developers can use the strings with these constants. For example: QPairConstants.ACTION_STATE_CHANGED for the “com.lge.qpair.STATE_CHANGED” action.

 

Registering a BroadcastReceiver

For broadcast intents from the QPair service to be received, a BroadcastReceiver needs to be registered.

Developers can choose between two methods: one is to declare a receiver in their AndroidManifest.xml files, the other is to create a BroadcastReceiver for QPair in their class.

 

The former method is used when the receiver always wants to receive QPair broadcast intents independently of any application. Please add the following codes to your in AndroidManifest.xml

 

<manifest>
    ...
    <receiver android:name=".QPairReceiver" android:permission="com.lge.qpair.permission.PROVIDE_QPAIR_SERVICE">
        <intent-filter>
            <action android:name="com.lge.qpair.CALL_FOR_PROPERTIES" />
            <action android:name="com.lge.qpair.STATE_CHANGED" />
            <action android:name="com.lge.qpair.CONNECTION_CHANGED" />
        </intent-filter>
    </receiver>
    ...
</manifest>

 

The android:permission attribute is optional. It used to restrict senders. See Modifying the AndroidManifest.xml File for details.

The latter method is used when a receiver wants to receive QPair broadcast intents only if the related application is running. Developers should register and unregister aBroadcastReceiver in their applications, generally in an Activity.

 

public class MyActivity {
private QPairReceiver myQPairReceiver = new QPairReceiver();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ... 
        // Register BroadcastReceiver for QPair
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.lge.qpair.CALL_FOR_PROPERTIES");
        filter.addAction("com.lge.qpair.STATE_CHANGED");
        filter.addAction("com.lge.qpair.CONNECTION_CHANGED");
        
        registerReceiver(myQPairReceiver, filter, 
                              "com.lge.qpair.permission.PROVIDE_QPAIR_SERVICE", null);
        ...
    }
    ...
}

 

The third argument of registerReceiver() is optional. It used to restrict senders.

 

Implementing a BroadcastReceiver

Whenever a BroadcastReceiver receives an intent, onReceive() is invoked. Developers should implement their own BroadcastReceiver to handle QPair broadcast intents.

 

The following is an example:

 

 
public class QPairReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // handle the changes of QPair connection status
        if( QPairConstants.ACTION_CONNECTION_CHANGED.equals(action) &&
              !intent.getBooleanExtra(QPairConstants.EXTRA_QPAIR_IS_ON, false) )
        {
            Toast.makeText(context, "QPair is Off.",
                                                      Toast.LENGTH_SHORT).show();
        }
        // handle the changes of QPair’s on/off
        else if( QPairConstants.ACTION_STATE_CHANGED.equals(action) &&
          intent.getBooleanExtra(QPairConstants.EXTRA_QPAIR_CONNECTED, false) )
        {
            Toast.makeText(context, "Connection is established.", 
                                                      Toast.LENGTH_SHORT).show();
        }
    }
    ...
}

 

The detailed implementation depends on the purpose of the application.

 

Navigation