publicfinalclassConstants{...// Defines a custom Intent actionpublicstaticfinalStringBROADCAST_ACTION="com.example.android.threadsample.BROADCAST";...// Defines the key for the status "extra" in an IntentpublicstaticfinalStringEXTENDED_DATA_STATUS="com.example.android.threadsample.STATUS";...}publicclassRSSPullServiceextendsIntentService{.../* * Creates a new Intent containing a Uri object * BROADCAST_ACTION is a custom Intent action */IntentlocalIntent=newIntent(Constants.BROADCAST_ACTION)// Puts the status into the Intent.putExtra(Constants.EXTENDED_DATA_STATUS,status);// Broadcasts the Intent to receivers in this app.LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);...}
// Broadcast receiver for receiving status updates from the IntentServiceprivateclassResponseReceiverextendsBroadcastReceiver{// Prevents instantiationprivateDownloadStateReceiver(){}// Called when the BroadcastReceiver gets an Intent it's registered to receive@publicvoidonReceive(Contextcontext,Intentintent){.../* * Handle Intents here. */...}}
// Class that displays photospublicclassDisplayActivityextendsFragmentActivity{...publicvoidonCreate(BundlestateBundle){...super.onCreate(stateBundle);...// The filter's action is BROADCAST_ACTIONIntentFiltermStatusIntentFilter=newIntentFilter(Constants.BROADCAST_ACTION);// Adds a data filter for the HTTP schememStatusIntentFilter.addDataScheme("http");...
// Instantiates a new DownloadStateReceiverDownloadStateReceivermDownloadStateReceiver=newDownloadStateReceiver();// Registers the DownloadStateReceiver and its intent filtersLocalBroadcastManager.getInstance(this).registerReceiver(mDownloadStateReceiver,mStatusIntentFilter);...
/* * Instantiates a new action filter. * No data filter is needed. */statusIntentFilter=newIntentFilter(Constants.ACTION_ZOOM_IMAGE);...// Registers the receiver with the new filterLocalBroadcastManager.getInstance(getActivity()).registerReceiver(mDownloadStateReceiver,mIntentFilter);