Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does setAction () do for intent (Broadcast)

What does setAction () do in intent (Service)

I don't really get what setAction () does, I mostly found it in "service to activity data passing" example. Can the string be set freely ?. What does it do exactly ?

When a broadcast intent is created, it must include an ACTION STRING in addition to optional data and a category string. As with standard intents, data is added to a broadcast intent using key-value pairs in conjunction with the putExtra() method of the intent object. The optional category string may be assigned to a broadcast intent via a call to the addCategory() method.

The action string, which identifies the broadcast event, must be unique and typically uses the application’s Java package name syntax. For example, the following code fragment creates and sends a broadcast intent including a unique action string and data:

 Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
intent.putExtra("HighScore", 1000); sendBroadcast(intent);

Another variation I've seen is :

 Intent broadcastIntent = new Intent();

            broadcastIntent.setAction("com.truiton.broadcast.string");
            broadcastIntent.putExtra("Data", "Broadcast Data");
            sendBroadcast(broadcastIntent);

            try {
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            broadcastIntent.setAction("com.truiton.broadcast.integer");
            broadcastIntent.putExtra("Data", 10);
            sendBroadcast(broadcastIntent);

            try {
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            broadcastIntent .setAction("com.truiton.broadcast.arraylist");
            broadcastIntent.putExtra("Data", mList);
            sendBroadcast(broadcastIntent);

this looks to more like indentify the incoming data type.

Does it mean to identifies event, incoming data type, an action or each Intent creation ? Can it be set free?

like image 249
Plain_Dude_Sleeping_Alone Avatar asked Jul 08 '16 06:07

Plain_Dude_Sleeping_Alone


1 Answers

Using the <action> tag inside <activity> tag to set an action in your Manifest.xml file is same as setting it programmatically using intent.setAction inside the java file.

These are generally used for Broadcast Receivers.

The following is an xml example:

<receiver android:name="MyReceiver" >
            <intent-filter>
                <action android:name="com.example.SendBroadcast" >
                </action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" >          
                </action>
            </intent-filter>
        </receiver>

But when you want a BroadCastReceiver to register and unregister programatically then setAction() can be used.

Intent intent = new Intent();
          intent.setAction("com.example.SendBroadcast");
          intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
          sendBroadcast(intent);

For more information just check: http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers

like image 178
Saini Avatar answered Nov 20 '22 19:11

Saini