Sunday, March 18, 2012

Blog Update
Custom Broadcasts:

Unlike system originated broadcasts, these are the broadcasts which are sent by user. Usually these are useful in communicating back with activity from services.

Step 1: Write the below code where ever you want to send a custom broadcast:

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.vl.dumptruck.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);

In above code, we are creating a Intent with custom action and broadcasting it.

Step 2: Write the below code in the class which will be responsible for receiving broadcasts:
private
BroadcastReceiver logoutReceiver;
IntentFilter logoutIntentFilter = new IntentFilter();
logoutIntentFilter.addAction("com.vl.dumptruck.ACTION_LOGOUT");
logoutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             //perform the task here
        }
};
registerReceiver(logoutReceiver, logoutIntentFilter);

1. In the above code, we are creating an IntentFilter with the action same as specified when sending the broadcast.
2. And a BroadcastReceiver, which is responsible for performing a task upon receing the broadcast(override the onReceive() method).
3. Now, register the broadcast using registerReceiver() method by passing BroadcastReceiver and IntentFilter objects. Here receiver is to call the callback method onReceive() and intent filter is to check whether the the receiver is authorized to respond to the broadcasted intent.
4. Finally, unregister the broadcast receiver using unregisterReceiver() by passing the receiver object, otherwise you will get a broadcast leaked exception.

        unregisterReceiver(logoutReceiver);

Note: Be careful with registering and unregistering broadcasts.
1. If you register a broadcast in onCreate(), then you have to unregister it in onDestroy(), with this the activity is capable of receiving the broadcasts even when activity is in background(i.e, in onPause() state also).
2. If you register a broadcast in onCreate() and you dont want to respond to broadcasts when you are in background(onPause()), then unregister your receiver in onPause() and register again in onResume() to start receiving broadcasts again and finally unregister it in onDestroy().



Move Android Apps Installation to SD Card
Please start using the tag name  android:installation as shown below to enable the option  to move the installation files to SDCard.
           package="xxx.xx.xxxx"
          android:versionCode="1"
           android:versionName="0.9.6" android:installLocation= "auto" >
This property is introduced in Introduced in  API Level 8.

No comments:

Post a Comment