Sunday, August 14, 2016

sendBroadcast Android Example

This is an example of using method sendBroadcast() to send broadcasts in Android. App has one BroadcastReceiver. It display broadcast action if there is one else display nothing. It also display the addition of two numbers that were sent to it. 

BroadcastReceiver has been implemented with the class NumberReceiver. So it must be registered by using the manifest file.

<receiver
    android:name=".NumberReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
       <action android:name="com.blogspot.nipunswritings.demosendbcast.add" />
    </intent-filter>
</receiver>

It can receive intents with the below action

com.blogspot.nipunswritings.demosendbcast.add

Project has class Constants that has been defined key for intent actions and extras.

public class Constants { 
    public static String ACTION_ADD = "com.blogspot.nipunswritings.demosendbcast.add"; 
    public static String EXTRA_X = "com.blogspot.nipunswritings.demosendbcast.x"; 
    public static String EXTRA_Y = "com.blogspot.nipunswritings.demosendbcast.y";
}

In the activity that invoke sendBroadcast() has Two EditTexts and a Button. When user click the button numbers in two EditTexts are send to BroadcastReceiver after converting them to integers. This is the button onClick method.

public void sendToAddition (View view) {

    boolean withAction = false;

    int x = Integer.parseInt(xEt.getText().toString());
    int y = Integer.parseInt(yEt.getText().toString());

    Intent intent;

    if (withAction) {
        intent = new Intent(Constants.ACTION_ADD);
    } else {
        intent = new Intent(MainActivity.this, NumberReceiver.class);
    }
    intent.putExtra(Constants.EXTRA_X, x);
    intent.putExtra(Constants.EXTRA_Y, y);

    sendBroadcast(intent);
}

Important code snippet of above method is this.

    Intent intent;

    if (withAction) {
        intent = new Intent(Constants.ACTION_ADD);
    } else {
        intent = new Intent(MainActivity.this, NumberReceiver.class);
    }
    intent.putExtra(Constants.EXTRA_X, x);
    intent.putExtra(Constants.EXTRA_Y, y);
 
    sendBroadcast(intent); 

If Boolean variable withAction is true Intent instance is created with the ACTION_ADD in constants file. If it is not true since both Receiver and Activity in the same class Intent instance is created with the class. Then integer values which are got by casting String that are provided by EditTexts are put into Intent instance. Finally sendBroadcast() is called.

Method onReceive(Context, Intent) of NumberReceiver. has been written below.
 
@Overridepublic void onReceive(Context context, Intent intent) {
    int x = intent.getIntExtra(Constants.EXTRA_X, 0);
    int y = intent.getIntExtra(Constants.EXTRA_Y, 0);

    String action = intent.getAction();
    if (action != null) {
        Toast.makeText(context, "Action = "+action, Toast.LENGTH_LONG).
                show();
    } else {
        Toast.makeText(context, "Action isn't available", Toast.LENGTH_LONG).
                show();
    }


    Toast.makeText(context, "x + y = "+Calculator.performAddition(x,y), Toast.LENGTH_LONG).
            show();
}

It get extras and actions then display them with a Toast.

Calculations is done by using the Calculator class. Calculator class implementation is this.

public class Calculator {
    public static int performAddition (int x, int y) {
        return x+y;
    }
}


No comments:

Post a Comment