Broadcast Custom Intent

How to  Broadcast Custom  Intent?

If you want your application should  generate and send custom intent,use

sendBroadcast();
sendStickyBroadcast();

How to Create Custom Intent?

   Intent intent = new Intent();
   intent.setAction("com.wordpress.myandroidninja.CUSTOM_INTENT");
   sendBroadcast(intent);


How to Register Custom Intent?

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
   <intent-filter>
         <action android:name="com.wordpress.myandroidninja.CUSTOM_INTENT">
         </action>
      </intent-filter>
   </receiver>
</application>
 
Advertisements

Pending Intent

What is Pending Intent?

Pending intent is an object that wraps another intent object with the target action to be performed.

It can be used to launch an activity,service,Broadcast receiver.

It is passed to other application to perform desired actions.

Types of Pending Intent?

getActivity(Context,int,Intent ,int);
getBroadcast(Context,int,Intent ,int);
getService(Context,int,Intent ,int);

Arguments of Pending Intent?

this(Context) – the context in which the PendingIntent starts the activity.

requestCode – a private request code for the sender.

intent – An explicit intent of the activity to be launched.

PendingIntent.FLAG_CANCEL_CURRENT – If described PendingIntent already exists,then cancel first once,and n new pending Intent is generated.

Broadcast Receiver

What is broadcast receiver?

It is an Application component.
Allows you to register for system or application events.
All registered receiver for an events are notified by android runtime once this event happens.
The broadcast can be generated either by system or by other application.
App can listen important System events such as Low battery,new Messages,System Reboot.

How to register broadcast receiver?

1.Statically – Declare in manifest file.
1.1.Broadcast receiver is declared in manifest.xml file like other components of android
1.2.Can’t be Unregistered.

2.Dynamically – In Java code
2.1.Broadcast receiver is registered using register() method.
2.2.Can Unregistered later.

How to use broadcast receiver?

1.Create broad cast receiver.

1.1.Broadcast receiver extend BroadcastReceiver class.
1.2.Override onReceive() method by default.

 public class MyReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
 }
 }

2.Register broad cast receiver.
2.1.An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file.
2.2.Register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
 <action android:name="android.intent.action.BOOT_COMPLETED">
</action>
      </intent-filter>
</receiver>
</application>